码迷,mamicode.com
首页 > Web开发 > 详细

WEB 项目中JMS的简单应用

时间:2017-08-31 15:56:37      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:cto   exce   des   队列   cat   nbsp   vax   instance   应用   

1.现在pom.xml 文件中引入所需的依赖

技术分享

2. 配置application.xml

<!-- Activemq 连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg value="admin" />
<constructor-arg value="admin" />
<constructor-arg value="failover:(tcp://192.168.100.109:61616)?timeout=2000" />
</bean>

<!-- ConnectionFactory Definition -->
<bean id="amqConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="activeMQConnectionFactory" />
</bean>

<!-- Default Destination Queue Definition -->
<!-- 测试配置多个Destination -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="MC_MESSAGE" />
</bean>

<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="amqConnectionFactory" />
<property name="defaultDestination" ref="destination" />
</bean>

<!-- Message Sender Definition -->
<bean id="messageSender" class="com.xxx.config.MessageSender">
<constructor-arg index="0" ref="jmsTemplate" />
<constructor-arg index="1" ref="destination" />
</bean>
<!-- 消息监听器,主要监听的目的地址 Message Receiver Definition -->
<bean id="messageReceiver" class="com.xxx.config.MessageReceiver">
</bean>
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="amqConnectionFactory" />
<property name="destinationName" value="MC_MESSAGE" />
<property name="messageListener" ref="messageReceiver" />
</bean>

3. 书写MessageSender 

package com.xxx.config;

import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {
private final JmsTemplate jmsTemplate;
private final Destination destination;

public MessageSender(final JmsTemplate jmsTemplate, final Destination destination) {
this.jmsTemplate = jmsTemplate;
this.destination = destination;
}

public void send(final String text) {
try {
jmsTemplate.setDefaultDestination(destination);
jmsTemplate.convertAndSend(text);
// System.out.println("发送消息 : " + text);
} catch (Exception e) {
e.printStackTrace();
}
}
}

4.消息MessageReceiver 

package com.xxx.config;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MessageReceiver implements MessageListener {

public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
textMessage.getText();
// System.out.println("接收到消息: " + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}

}

 5接口.调用 即可将消息发送到指定的消息队列当中

@Resource(name="messageSender")
private MessageSender messageSender;

//定义消息实体,存储内容,并将实体转化为字符串  调用send方法 将消息发送出去,同时消息监听会打印监听到的消息

QueneVo Vo = new QueneVo();
Vo.setId(problem.getProblemId());
Vo.setTitle("创建了新问题【"+problem.getProblemId()+"】,需要您处理");
Vo.setContent(problem.getProblemDescribe());
Vo.setMsgTime(DateUtil.getTime(0));
this.messageSender.send(JsonUtils.writeObject(Vo));

 

WEB 项目中JMS的简单应用

标签:cto   exce   des   队列   cat   nbsp   vax   instance   应用   

原文地址:http://www.cnblogs.com/7426QS/p/7359753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!