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));