JMS - ActiveMQ集成Spring

时间:2014-11-03 19:14:04   收藏:0   阅读:328

下面是ActiveMQ官网提供的文档。
http://activemq.apache.org/spring-support.html

下面是我添加的一些dependency:

<!-- jms activemq -->
<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>${activemq.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>${activemq.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>${activemq.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${spring.version}</version>
</dependency>

 

maven中添加时要注意还有个xbean-spring;
之前并没有注意,运行发现异常提示ClassNotFound:org.apache.xbean.spring.context.v2.XBeanNamespaceHandler;
后来我添加了xbean-v2,结果提示v2c,于是我添加v2c,后来感觉太傻就加了xbean-spring。

配置方面可以使用jms和activeMq的标签:

xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"

 

相应的xsi:schemaLocation:

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd

 

关于connectionFactory的配置可以使用amq标签:

<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />

 

但是在这里我打算试试PooledConnectionFactory;
关于org.apache.activemq.pool.PooledConnectionFactory官网有以下解释:
If you are not using a JCA container to manage your JMS connections, we recommend you use our pooling JMS connection provider, (org.apache.activemq.pool.PooledConnectionFactory) from the activemq-pool library, which will pool the JMS resources to work efficiently with Spring‘s JmsTemplate or with EJBs.

 

对于其属性,下面根据javaDoc给出一些解释:
·MaximumActiveSessionPerConnection:每个Connection的最大Session数
·BlockIfSessionPoolIsFull:默认为session池满时请求获得session会阻塞;设置false则会抛出JMSException
·MaxConnections:最大连接数
·IdleTimeout:空闲时间,默认为30秒
·CreateConnectionOnStartup:是否在启动时创建connection

 

在这里我先用默认参数声明,不知道为什么总是报MalformPrameterizedType...

<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" />
上次用的队列,这次换用Topic试试...

<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
  <constructor-arg index="0" value="spartaTopic"></constructor-arg>
</bean>

 

当然也可以使用amq标签:

<amq:topic physicalName="sparta" />

 

如果是使用queue的话:

<amq:queue physicalName="sparta" />

 

难道我将这些放到spring里就是为了用用标签方便DI?
用<spring in action>里的话来说就是jmsTemplate是<spring对jms支持核心的部分>
(另有jmsTemplate102为适应jms1.0.2的);
和jdbcTemplate那样 jmsTemplate也有提供相似的优势。
比如,像jdbcTemplate处理失控的jdbc代码那样,用jmsTemplate处理失控的jms代码。
或者,如果在使用JmsTemplate是捕捉到了JMSException,JmsTemplate将捕获该异常,然后抛出一个Spring自带的JmsException的子类异常(个人感觉spring提供的不是更详细的异常信息,只是侧重点不同...)。

bubuko.com,布布扣

 

比如:
·ListenerExecutionFailedException:监听器执行失败
·MessageConversionException:消息转换失败
·SynchedLocalTransactionFailedException:同步本地事务未完成
·UncategorizedJmsException:没有适合异常的其他情况
如果我们catch了JMSException,我们依然可以把他转为JmsException:

catch (JMSException e) {
            e.printStackTrace();
            JmsException je = JmsUtils.convertJmsAccessException(e);
        }

 

现在试着配置jmsTemplate:

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" >
    <property name="connectionFactory" >
        <bean class="org.apache.activemq.pool.PooledConnectionFactory" />
    </property>
    <property name="defaultDestination" >
        <amq:topic physicalName="sparta" />
    </property>
</bean>

 

这样编写代码时就变得简单多了,之前那些connectionFactory,connection,session,consumer,producer统统不见了;
我只需要使用template:

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
JmsTemplate template = (JmsTemplate)context.getBean("jmsTemplate");
template.send(new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
        ActiveMQMapMessage msg = (ActiveMQMapMessage)session.createMapMessage();
        msg.setString("msg", "This is sparta!!");
        return msg;
    }
});

 

接收时只需要template.receive();

但需要注意!这个receive是同步接收消息的,他会一直阻塞到有消息个接收。
可能会想到MessageListener,比如我们可以给一个MessageConsumer对象setMessageListener:

MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
    public void onMessage(Message message) {
        try {
            System.out.println("listener catched:::"+((TextMessage)message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
});

 

上面代码中的MessageListener实例,如果新建一个专门用来监听的类,实现MessageListener接口并加上MessageDriven标注就会出现一个问题——他不够pojo。他有侵入性,我不想要任何实现接口的语法出现在代码中。

于是我可以用listener-container;
现在我创建一个类去监听,比如:

public class CustomedListener {
    void processHandle(HashMap<String,String> map){
        System.out.println("msg:::"+map.get("msg"));
    }
}

 

上面的publisher发送的message是ActiveMQMapMessage,这就需要我把参数定义为上面那种形式。
然后看一下spring中如何配置这个Listener:

<bean id="myListener" class="pac.testcase.jms.CustomedListener"/>
<jms:listener-container connection-factory="connectionFactory">
    <jms:listener destination="sparta" ref="myListener" method="processHandle"/>
</jms:listener-container>

 

这样我就不需要去调用receive了,有消息就接收。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!