先来温顾一下ActiveMQ5.0提供的主要功能:
1.ActiveMQ5.0消息中间件实现了JMS1.1规范
2.支持点对点、发布/订阅式消息;
3.支持持久消息;
开始tomcat5.5 + sprng2.0 + ActiveMQ的配置
目标:
1.tomcat启动时,也同时启时ActiveMQ服务
2.配置发送、接收消息的模板,使代码编写不必再配置发送、接收过程,而可以直接对消息做处理
参考文档:
http://activemq.apache.org
http://www.springside.org.cn/
一.准备工作:
所需jar
activeMQ
SPRING
XBEAN
jakarta-commons
...
二.Tomcat中启动ActiverMQ的配置(Spring整合)
----------------------------------------- web.xml -------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:incx/activemq/conf/spring-activeMQ-service.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
-------------------------------------------------------------------------------------------------
--------------------------- spring-activeMQ-service.xml ----------------------------
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.org/config/1.0"
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-2.0.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- Server configure -->
<!-- create an embedded ActiveMQ Broker -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:61616"/>
</amq:transportConnectors>
</amq:broker>
</beans>
-------------------------------------------------------------------------------------------------
至此,已经可以启动Tomcat使用ActiveMQ的服务了,调试可以使用ActiveMQ自带的消息发送和接收代码。
三.Spring中配置ActiveMQ连接工厂
<!-- ActiveMQ connectionFactory to use -->
<amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost" />
其中vm://localhost为ActiveMQ的服务器地址,如果是远程服务器应填写
<tcp|udp|...>://xxx.xxx.xxx:prot
四.Sprng中配置ActiveMQ消息目的地模板
<!-- ActiveMQ destinations to use -->
<amq:queue name="destination" physicalName="cjw.queue.test"/>
physicalName的值自定义。
五.Sprng中配置ActiveMQ消息转换
<!-- OrderMessage converter -->
<bean id="orderMessageConverter" class="incx.activemq.OrderMessageConverter"/>
--------------------------- OrderMessageConvert.java ---------------------------
package incx.activemq;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.jms.support.converter.MessageConverter;
public class OrderMessageConverter implements MessageConverter{
/**
* (non-Javadoc)
*
* @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object,
* javax.jms.Session)
*/
public Message toMessage(Object obj, Session session) throws JMSException {
//check Type
if (obj instanceof Order) {
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
HashMap map = new HashMap();
try {
//Order,Order,Product must implements Seralizable
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
bos.close();
map.put("Order", bos.toByteArray());
objMsg.setObjectProperty("Map", map);
} catch (IOException e) {
e.printStackTrace();
}
return objMsg;
} else {
throw new JMSException("Object:[" + obj + "] is not Order");
}
}
/**
* (non-Javadoc)
*
* @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message)
*/
public Object fromMessage(Message msg) throws JMSException {
if (msg instanceof ObjectMessage) {
HashMap map= (HashMap) ((ObjectMessage) msg)
.getObjectProperty("Map");
try {
// Order,Order,Product must implements Seralizable
ByteArrayInputStream bis=new ByteArrayInputStream((byte[]) map.get("Order"));
ObjectInputStream ois=new ObjectInputStream(bis);
return ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
} else {
throw new JMSException("Msg:[" + msg + "] is not Map");
}
}
}
-------------------------------------------------------------------------------------------------
--------------------------------------- Order.java ----------------------------------------
package incx.activemq;
import java.io.Serializable;
public class Order implements Serializable{
private static final long serialVersionUID = 1L;
private long id;
private long orderTime;
private String orderContent;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getOrderTime() {
return orderTime;
}
public void setOrderTime(long orderTime) {
this.orderTime = orderTime;
}
public String getOrderContent() {
return orderContent;
}
public void setOrderContent(String orderContent) {
this.orderContent= orderContent;
}
}
-------------------------------------------------------------------------------------------------
六.Sprng中配置消息基本模板
<!-- Spring JmsTemplate config -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<!-- lets wrap in a pool to avoid creating a connection per send -->
<bean class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory" />
</bean>
</property>
<!-- custom MessageConverter define -->
<property name="messageConverter" ref="orderMessageConverter"/>
</bean>
七.Sprng中配置ActiveMQ消息发送模板
<!-- POJO which send Message uses Spring JmsTemplate,bind JMSTemplate and Queue -->
<bean id="orderMessageProducer" class="incx.activemq.OrderMessageProducer">
<property name="template" ref="jmsTemplate"/>
<property name="destination" ref="destination"/>
</bean>
------------------------- OrderMessageProducer.java ---------------------------
package incx.activemq;
import javax.jms.Queue;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
/**
* 订单JMS消息发送类.
*
* @author cjw
*/
public class OrderMessageProducer{
private Logger logger = Logger.getLogger(this.getCass());
private JmsTemplate template;
private Queue destination;
public void setTemplate(JmsTemplate template) {
this.template = template;
}
public void setDestination(Queue destination) {
this.destination = destination;
}
public void send(Order order) {
template.convertAndSend(this.destination, order);
logger.debug("============= send Order is Over ==============");
}
}
-------------------------------------------------------------------------------------------------
八.Sprng中配置ActiveMQ消息接收监听
<!-- define MessageListenerAdapter -->
<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="incx.activemq.OrderMessageConsumer" />
<!-- <property name="mailService" ref="mailService"></property> -->
</constructor-arg>
<!-- mey be other method -->
<property name="defaultListenerMethod" value="sendEmail" />
<!-- custom MessageConverter define -->
<property name="messageConverter" ref="orderMessageConverter"/>
</bean>
------------------------- OrderMessageConsumer.java --------------------------
package incx.activemq;
import org.apache.log4j.Logger;
/**
* 订单JMS消息接受类. 接受Queue消息
*
* @author cjw
*/
public class OrderMessageConsumer {
// private MailService mailService;
private Logger logger = Logger.getLogger(this.getClass());
public void sendEmail(Order order) {
logger.debug("查看:" + order.getShipAddress());
// SimpleMailMessage msg = new SimpleMailMessage();
// msg.setTo(order.getCustomer().getEmail());
// msg.setFrom(Constants.SYSTEM_MAILBOX);
// msg.setSubject("订单通知");
// Map<String, Order> model = new HashMap<String, Order>();
// model.put("order", order);
// mailService.send(msg, "NotifyOrder.ftl", model);
}
public void setMailService(Object mailService) {
// this.mailService = mailService;
}
}
-------------------------------------------------------------------------------------------------
九.Sprng中配置ActiveMQ持久消息
待续....
九.JAVA测试代码
待续....
ActiveMQ5.0学习笔记 2 tomcat5.5 + sprng2.0 + ActiveMQ5.0
最新推荐文章于 2024-08-14 18:26:28 发布