Java連接ActiveMQ代碼示例(Producer和Consumer)
阿新 • • 發佈:2018-03-28
生產 exce org 默認 main isp 用戶 close trac
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class JMSProducer { //默認連接用戶名 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; //默認連接密碼 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;JMSProducer//默認連接地址 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; //發送的消息數量 private static final int SENDNUM = 10; public static void main(String[] args) { //連接工廠 ConnectionFactory connectionFactory; //連接 Connection connection = null;//會話 接受或者發送消息的線程 Session session; //消息的目的地 Destination destination; //消息生產者 MessageProducer messageProducer; //實例化連接工廠 connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL); try {//通過連接工廠獲取連接 connection = connectionFactory.createConnection(); //啟動連接 connection.start(); //創建session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); //創建一個名稱為HelloWorld的消息隊列 destination = session.createQueue("HelloWorld"); //創建消息生產者 messageProducer = session.createProducer(destination); //發送消息 sendMessage(session, messageProducer); session.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ if(connection != null){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * 發送消息 * @param session * @param messageProducer 消息生產者 * @throws Exception */ public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{ for (int i = 0; i < JMSProducer.SENDNUM; i++) { //創建一條文本消息 TextMessage message = session.createTextMessage("消息提供者發送到MQ的第" + i + "個報文!"); System.out.println("消息提供者發送到MQ的第" + i + "個報文!"); //通過消息生產者發出消息 messageProducer.send(message); } } }
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class JMSConsumer { private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默認連接用戶名 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默認連接密碼 private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;//默認連接地址 public static void main(String[] args) { ConnectionFactory connectionFactory;//連接工廠 Connection connection = null;//連接 Session session;//會話 接受或者發送消息的線程 Destination destination;//消息的目的地 MessageConsumer messageConsumer;//消息的消費者 //實例化連接工廠 connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL); try { //通過連接工廠獲取連接 connection = connectionFactory.createConnection(); //啟動連接 connection.start(); //創建session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //創建一個連接HelloWorld的消息隊列 destination = session.createQueue("HelloWorld"); //創建消息消費者 messageConsumer = session.createConsumer(destination); while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if(textMessage != null){ System.out.println("收到的消息:" + textMessage.getText()); }else { break; } } } catch (JMSException e) { e.printStackTrace(); } } }JMSConsumer
Java連接ActiveMQ代碼示例(Producer和Consumer)