测试mq消费者

This commit is contained in:
shuzheng 2016-11-24 23:16:42 +08:00
parent dd78fd64b9
commit c9699653a4
4 changed files with 59 additions and 45 deletions

View File

@ -85,7 +85,7 @@
</webApp> </webApp>
<connectors> <connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8088</port> <port>8080</port>
</connector> </connector>
</connectors> </connectors>
<reload>automatic</reload> <reload>automatic</reload>

View File

@ -1,9 +1,15 @@
package com.zheng.cms.controller; package com.zheng.cms.controller;
import com.zheng.common.util.JmsUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.jms.Destination;
/** /**
* 消息队列controller * 消息队列controller
@ -16,19 +22,22 @@ public class ActiveMQController extends BaseController {
private static Logger _log = LoggerFactory.getLogger(ActiveMQController.class); private static Logger _log = LoggerFactory.getLogger(ActiveMQController.class);
// @Autowired @Autowired
// JmsTemplate jmsTemplate; JmsTemplate jmsQueueTemplate;
//
// @Autowired @Autowired
// Destination defaultQueueDestination; Destination defaultQueueDestination;
//
// @RequestMapping("/send") @RequestMapping("/send")
// @ResponseBody @ResponseBody
// public Object send() { public Object send() {
// for (int i = 0; i < 1000; i ++) { long start = System.currentTimeMillis();
// JmsUtil.sendMessage(jmsTemplate, defaultQueueDestination, "消息" + i); for (int i = 0; i < 100; i ++) {
// } _log.info("发送消息" + (i + 1));
// return "success"; JmsUtil.sendMessage(jmsQueueTemplate, defaultQueueDestination, "消息" + (i + 1));
// } }
_log.info("发送消息消耗时间" + (System.currentTimeMillis() - start));
return "success";
}
} }

View File

@ -27,8 +27,8 @@ public class defaultQueueMessageListener implements MessageListener {
public void run() { public void run() {
TextMessage textMessage = (TextMessage) message; TextMessage textMessage = (TextMessage) message;
try { try {
_log.info("defaultQueueMessageListener接收到:{}", textMessage.getText()); _log.info("cms-web接收到:{}", textMessage.getText());
} catch (JMSException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -5,38 +5,43 @@
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
default-autowire="byName"> default-autowire="byName">
<!--&lt;!&ndash; 连接工厂 &ndash;&gt;--> <!-- 连接工厂 -->
<!--<bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">--> <bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--<property name="brokerURL" value="${AvtiveMQ.brokerURL}"/>--> <property name="brokerURL" value="${AvtiveMQ.brokerURL}"/>
<!--</bean>--> </bean>
<!--<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">--> <!--<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">-->
<!--<property name="connectionFactory" ref="activeMqConnectionFactory"/>--> <!--<property name="connectionFactory" ref="activeMqConnectionFactory"/>-->
<!--<property name="maxConnections" value="10"/>--> <!--<property name="maxConnections" value="10"/>-->
<!--</bean>--> <!--</bean>-->
<!--<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">--> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!--<property name="targetConnectionFactory" ref="pooledConnectionFactory"/>--> <property name="targetConnectionFactory" ref="activeMqConnectionFactory"/>
<!--</bean>--> <property name="sessionCacheSize" value="100"/>
<!--&lt;!&ndash; 点对点队列 &ndash;&gt;--> </bean>
<!--<bean id="defaultQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">--> <!-- 点对点队列 -->
<!--<constructor-arg index="0" value="com.zheng.cms.queue.default" />--> <bean id="defaultQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!--</bean>--> <constructor-arg index="0" value="com.zheng.cms.queue.default"/>
<!--&lt;!&ndash; 一对多队列 &ndash;&gt;--> </bean>
<!--&lt;!&ndash;--> <!-- 一对多队列 -->
<!--<bean id="defaultTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">--> <bean id="defaultTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<!--<constructor-arg index="0" value="com.zheng.cms.topic.default" />--> <constructor-arg index="0" value="com.zheng.cms.topic.default"/>
<!--</bean>--> </bean>
<!--&ndash;&gt;-->
<!--&lt;!&ndash; 生产者 &ndash;&gt;--> <!-- 生产者 -->
<!--<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">--> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!--<property name="connectionFactory" ref="connectionFactory"/>--> <property name="connectionFactory" ref="connectionFactory"/>
<!--</bean>--> <property name="pubSubDomain" value="false" />
<!--&lt;!&ndash; 消费者 &ndash;&gt;--> </bean>
<!--<bean id="defaultQueueMessageListener" class="com.zheng.cms.jms.defaultQueueMessageListener"/>--> <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!--<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">--> <property name="connectionFactory" ref="connectionFactory"/>
<!--<property name="connectionFactory" ref="connectionFactory" />--> <property name="pubSubDomain" value="true" />
<!--<property name="destination" ref="defaultQueueDestination" />--> </bean>
<!--<property name="messageListener" ref="defaultQueueMessageListener" />-->
<!--</bean>--> <!-- 消费者 -->
<bean id="defaultQueueMessageListener" class="com.zheng.cms.jms.defaultQueueMessageListener"/>
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="defaultQueueDestination"/>
<property name="messageListener" ref="defaultQueueMessageListener"/>
</bean>
</beans> </beans>