==============发送方配置(与接受方配置差别在于不要配置服务器)=======
http://hellohank.iteye.com/blog/1341290
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置ActiveMQ服务 -->
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<!-- 提供的连接方式有:VM Transport、TCP Transport、SSL Transport、
Peer Transport、UDP Transport、Multicast Transport、HTTP and HTTPS Transport、
Failover Transport、Fanout Transport、Discovery Transport、ZeroConf Transport等 -->
<amq:transportConnector uri="tcp://127.0.0.1:61616" />
</amq:transportConnectors>
</amq:broker>
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverter" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducer">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumer">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
===========接收方配置============
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-5.0.0.xsd">
<!-- 配置JMS连接工厂(注:brokerURL是关键,
它应该是上面的amq:transportConnectors里面的值之一) -->
<amq:connectionFactory id="jmsConnectionFactory"
brokerURL="tcp://127.0.0.1:61616" />
<!-- 消息发送的目的地(注:amq:queue是用于指定是发送topic不是queue,对应上面配置中的amq:destinations) -->
<amq:queue name="destination" physicalName="ossQueue" />
<!-- 创建JMS的Session生成类 -->
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<!-- 指定发送信息时使用的消息转换类.
这个选项不填的话,默认的是:SimpleMessageConverter,它只支持4种类型的对象:String, byte[],Map,Serializable
-->
<!-- 如果加上下面这段配置就会出错, 错误原因是Book不是一个原始类, 但我已经将它继承Serializable了,可还是不行, 我想可能有其他什么原因吧, 但我现在不清楚 -->
<!-- <property name="messageConverter"
ref="resourceMessageConverter" /> -->
</bean>
<!-- 发送消息的转换类
(这个类要继承org.springframework.jms.support.converter.MessageConverter) -->
<bean id="resourceMessageConverter"
class="com.focustech.jms.ResourceMessageConverterb" />
<!-- 消息生产者(通过指定目的地, 就可以同时指定其发送的消息模式是topic还是queue) -->
<bean id="resourceMessageProducer"
class="com.focustech.jms.ResourceMessageProducerb">
<property name="template" ref="jmsTemplate" />
<property name="destination" ref="destination" />
</bean>
<!-- 消息接收类(这个类需要继承,当然也可以通过MessageListenerAdapter指定消息转换器来实现用户自定义的消息收发) -->
<bean id="resourceMessageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean
class="com.focustech.jms.ResourceMessageConsumerb">
</bean>
</constructor-arg>
<property name="defaultListenerMethod" value="recieve" />
<!-- 自定义接收类与接收的方法 -->
<property name="messageConverter"
ref="resourceMessageConverter" />
</bean>
<!-- 消息监听容器,其各属性的意义为:
connectionFactory:指定所监听的对象,在这里就是监听连接到tcp://127.0.0.1:61616上面的ActiveMQ;
destination:监听的消息模式;
messageListener:接收者
-->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
</bean>
</beans>
==========web.xml=====
<!-- spring配置 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
=====java===
package com.focustech.jms;
import java.io.Serializable;
public class Book implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -6988445616774288928L;
long id;
String name;
String author;
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
package com.focustech.jms;
public class ResourceMessageConsumer
{
public void recieve(Object obj)
{
Book book = (Book) obj;
System.out.println("============qqqqq===========================");
System.out.println("receiveing message qqqq...");
System.out.println(book.toString());
System.out.println("here to invoke our business methodqqqq...");
System.out.println("====================qqq===================");
}
}
package com.focustech.jms;
import java.awt.print.Book;
import java.util.HashMap;
import java.util.Map;
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.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
public class ResourceMessageConverter implements MessageConverter
{
@SuppressWarnings("unchecked")
public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException
{
// check Type
if (obj instanceof Book)
{
// 采用ActiveMQ的方式传递消息
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
Map map = new HashMap();
map.put("Book", obj);
// objMsg.setObjectProperty里面放置的类型只能是:String, Map, Object, List
objMsg.setObjectProperty("book", map);
return objMsg;
}
else
{
throw new JMSException("Object:[" + obj + "] is not Book");
}
}
public Object fromMessage(Message msg) throws JMSException, MessageConversionException
{
if (msg instanceof ObjectMessage)
{
Object obj = ((ObjectMessage) msg).getObject();
return obj;
}
else
{
throw new JMSException("Msg:[" + msg + "] is not Map");
}
}
}
package com.focustech.jms;
import javax.jms.Destination;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ResourceMessageProducer
{
private JmsTemplate template;
private Destination destination;
public JmsTemplate getTemplate()
{
return template;
}
public void setTemplate(JmsTemplate template)
{
this.template = template;
}
public Destination getDestination()
{
return destination;
}
public void setDestination(Destination destination)
{
this.destination = destination;
}
public void send(Book book)
{
System.out.println("=======================================");
System.out.println("do send ......");
long l1 = System.currentTimeMillis();
template.convertAndSend(this.destination, book);
System.out.println("send time:" + (System.currentTimeMillis() - l1) / 1000 + "s");
System.out.println("=======================================");
}
}
==jsp==
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.jms.JmsException" %>
<%@ page import="com.focustech.jms.Book" %>
<%@ page import="com.focustech.jms.ResourceMessageProducer" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%
try {
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
ResourceMessageProducer resourceMessageProducer = (ResourceMessageProducer) wac.getBean("resourceMessageProducer");
Book book = new Book();
book.setId(123);
book.setName("jms test!");
book.setAuthor("taofucheng");
resourceMessageProducer.send(book);
} catch (JmsException e) {
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
集成事务
Spring提供的JMS的API中已经有了集成事务的功能,我们只要将上面监听容器的配置改成下面的就行了:
首先,将jmsTemplate设置成支持事务(它默认是不支持事务的):
<bean id="jmsTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory"
ref="jmsConnectionFactory" />
</bean>
</property>
<property name="sessionTransacted" value="true"/>
</bean>
然后再在消息监听容器中设置指定的事务管理:
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="resourceMessageListener" />
<!—jtaTransactionManager是系统中的事务管理类,在我们的系统中,是由Spring托管的 -->
<property name="transactionManager" ref="jtaTransactionManager" />
</bean>
这样配置之后,当事务发生回滚时,消息也会有回滚,即不发送出去。
4、其它高级应用
ActiveMQ还有许多其它高级的应用,如:自动重连机制,也就是保证当通信双方或多方的链接断裂后它会根据用户的设置自动连接,以保证建立可靠的传输;另外,ActiveMQ还有其它方式嵌入到Spring中,如它可以通过xbean, file等方式建立应用;它还可以通过JMX对消息的发送与接收进行实时查看;消息的确认方式等等,还有很多高级的应用,请参考
本文附件下载:
- src.rar (4.4 KB)
- srcb.rar (3.6 KB)
已有
0 人发表留言,猛击->>
这里<<-参与讨论
ITeye推荐