標籤: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的簡單應用