测试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>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8088</port>
<port>8080</port>
</connector>
</connectors>
<reload>automatic</reload>

View File

@ -1,9 +1,15 @@
package com.zheng.cms.controller;
import com.zheng.common.util.JmsUtil;
import org.slf4j.Logger;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.jms.Destination;
/**
* 消息队列controller
@ -16,19 +22,22 @@ public class ActiveMQController extends BaseController {
private static Logger _log = LoggerFactory.getLogger(ActiveMQController.class);
// @Autowired
// JmsTemplate jmsTemplate;
//
// @Autowired
// Destination defaultQueueDestination;
//
// @RequestMapping("/send")
// @ResponseBody
// public Object send() {
// for (int i = 0; i < 1000; i ++) {
// JmsUtil.sendMessage(jmsTemplate, defaultQueueDestination, "消息" + i);
// }
// return "success";
// }
@Autowired
JmsTemplate jmsQueueTemplate;
@Autowired
Destination defaultQueueDestination;
@RequestMapping("/send")
@ResponseBody
public Object send() {
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i ++) {
_log.info("发送消息" + (i + 1));
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() {
TextMessage textMessage = (TextMessage) message;
try {
_log.info("defaultQueueMessageListener接收到:{}", textMessage.getText());
} catch (JMSException e) {
_log.info("cms-web接收到:{}", textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}

View File

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