xml----------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 配置生产者 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!--这个是队列目的地,点对点的 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>wgq_queue</value>
</constructor-arg>
</bean>
<!-- 接收消息 -->
<!-- 配置监听器 -->
<bean id="myMessageListener" class="com.tms.activemq.ActiveMQListener" />
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="myMessageListener" />
</bean>
</beans>
发送--------------------------------------------------
//持久化到数据库
jmsTemplate.setDeliveryPersistent(true);
//jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < listfp.size(); i++) {
JSONObject newfp = new JSONObject();
JSONArray newfp_list = new JSONArray();
newfp_list.add(listfp.get(i));
newfp.put("list", newfp_list);
newfp.put("ret", ret);
newfp.put("msg", msg);
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(newfp.toJSONString());
logger.info("发送...:" + textMessage);
return textMessage;
}
接收----------------------------------------------------------------------------
public class ActiveMQListener implements MessageListener {
private OIDocCenterService oiDocCenterService = null;
private static Logger logger = Logger.getLogger(ActiveMQListener.class.getName());
@Override
public void onMessage(Message message) {
if(oiDocCenterService==null)
oiDocCenterService=(OIDocCenterService) SystemInitListener.getAppContext().getBean("oiDocCenterService",IOIDocCenterService.class);
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
logger.info("接收到mq的发票信息:" + text);
JSONObject fapobj=JSON.parseObject(text);
JSONArray list = fapobj.getJSONArray("list");
String ret = (String) fapobj.get("ret");
String msg = (String) fapobj.get("msg");
ServerGetInvoiceList invoicelist = JSON.parseObject(list.getString(0), ServerGetInvoiceList.class);
ServerGetInvoice invoice = invoicelist.getInvoice();
// 调用发票同步接口
oiDocCenterService.doServerPut(invoice,ret,msg);
} catch (JMSException e) {
e.printStackTrace();
}
}