例行demo,記錄 java
package com.test.activeMQ; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; /** * @ClassName: TestSender * @Description:消息發送 * @date 2015-7-24 * @version 1.0 * @since JDK1.6 */ public class TestSender{ // 鏈接工廠 private ConnectionFactory connectionFactory; private Connection connection; private Session session; // 隊列 private Destination destination; //異步發送 private MessageProducer messageProducer; /** * @Title: sendMsg * @param: msg * @return:void * @Description:初始化鏈接 */ public void sendMsg(String msg) { try { connection = connectionFactory.createConnection(); connection.start(); // 第一個參數非事務性 ,第二個參數表示接收消息後會自動答覆 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 建立隊列 destination = session.createQueue("testQueue"); // 建立發送者 messageProducer = session.createProducer(destination); //建立發送 createMsg(session, messageProducer, msg); } catch (JMSException e) { e.printStackTrace(); } finally { try { if(null != connection) connection.close(); } catch (Throwable ignore) { } } } /** * @Title: createMsg * @return:void * @Description:建立發送消息 */ private void createMsg(Session session, MessageProducer messageProducer, String msg) { try { TextMessage message = session.createTextMessage("testQueue 消息:" + msg); System.out.println("我是發送信息:" + msg); messageProducer.send(message); } catch (JMSException e) { e.printStackTrace(); } } public void setConnectionFactory(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; } }
package com.test.activeMQ; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /** * @ClassName: TestListerner * @Description:異步監聽消息 * @date 2015-7-24 * @version 1.0 * @since JDK1.6 */ class TestListerner implements MessageListener { /** * @Title: onMessage * @return:void * @Description:有消息時自動觸發 * @throws Exception */ public void onMessage(Message message) { try { System.out.println("========我是接受分隔線==========="); System.out.println(((TextMessage)message).getText()); } catch (JMSException e) { System.out.println("接受異常"); } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!--建立鏈接工廠 --> <!--可選 鏈接池 org.apache.activemq.pool.PooledConnectionFactory --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!--failover斷線重連機制 --> <property name="brokerURL" value="failover:(tcp://localhost:61616)" /> </bean> <!--發送者 --> <bean id="testSender" class="com.test.activeMQ.TestSender"> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!--入口Action --> <bean id="sendAction" class="com.test.activeMQ.action.TestAction"> <property name="testSender" ref="testSender"/> </bean> <!-- 建立消息隊列 --> <bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 隊列名 --> <constructor-arg> <value>testQueue</value> </constructor-arg> </bean> <!--同步方式監聽 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> 超時時間 <property name="receiveTimeout" value="6000" /> </bean>--> <!-- 消息隊列監聽,實現接收消息onMessage方法 --> <bean id="testListener" class="com.test.activeMQ.TestListerner"></bean> <!-- 異步方式消息監聽容器,需配置鏈接工廠,被監聽隊列,監聽器 --> <bean id="testListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="testQueue" /> <property name="messageListener" ref="testListener" /> </bean> </beans>
同步 spring
http://blog.csdn.net/moonsheep_liu/article/details/6683190
apache
異步 session
http://blog.csdn.net/moonsheep_liu/article/details/6684948
異步