activemq有两种方式提供服务:一种是以tcp单独启动服务另一种是内嵌jvm形式
两种形式配置分别为 "tcp://localhost:61616","vm://localhost:61616".
默认端口为61616
1、引入主要java包
spring-2.5.4.jar
activemq-all-5.2.0.jar
2、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 消息中介-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost:61616"/>
<property name="useAsyncSend" value="true"/>
</bean>
<!-- 操作日志队列目的地-->
<bean id="logQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="logQueue"/>
</bean>
<bean id="logJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 设置默认的消息目的地-->
<property name="defaultDestination" ref="logQueue"/>
<!-- 由于receiver方法是同步的,所以我们在这里对接收设置超时时间-->
<property name="receiveTimeout" value="60000"/>
</bean>
<!-- 消息发送者-->
<bean id="producer" class="com.hangyjx.common.log.JmsProducer">
<property name="jmsTemplate" ref="logJmsTemplate"/>
</bean>
<!-- 消息接收监听器用于异步接收消息-->
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="logQueue"/>
<property name="messageListener" ref="logMDPAdapter"/>
</bean>
<bean id="logMDPAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="logJMSMDP"/>
<property name="defaultListenerMethod" value="process"/>
</bean>
<!-- 消息驱动pojo -->
<bean id ="logJMSMDP" class="com.hangyjx.common.log.LogJmsMDP" >
</bean>
</beans>
3、
package com.hangyjx.common.log;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class JmsProducer {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
// 传送一个Student 对象(重写了toString()方法)
public void send(final Map<String,Object> mess) {
this.jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
// message.setString("key1", student.getName());
// message.setString("key2", student.getAge());
// //也可以直接发送对象,对象必须是可序列化的
// ObjectMessage message = session.createObjectMessage();
// message.setObject(o);
System.out.println("生产消息一条:");
System.out.println(mess);
System.out.println("生产消息一条.");
message.setObject("msg", mess);
return message;
}
});
}
}
4、
package com.hangyjx.common.log;
import java.util.Map;
import com.hangyjx.core.dao.DBService;
public class LogJmsMDP {
@SuppressWarnings("unchecked")
public void process(Map<String,Object> map){
if(map != null && !map.isEmpty() && map.containsKey("msg")){
Map<String,Object> mess = (Map<String,Object>)map.get("msg");
if(!mess.isEmpty() && mess.containsKey("sqlId")){
System.out.println("消费消息一条:");
System.out.println(mess);
System.out.println("消费消息一条.");
}
}
}
}