JMS-ACTIVEMQ Integrated Spring

Source: Internet
Author: User
Tags amq

Below is the documentation provided by the ACTIVEMQ website.
Http://activemq.apache.org/spring-support.html

Here are some of the dependency I added:

<!--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>

There is a xbean-spring to be aware of when adding in Maven.
Before the notice, run found abnormal hint ClassNotFound:org.apache.xbean.spring.context.v2.XBeanNamespaceHandler;
Later I added xbean-v2, the results hint v2c, so I add v2c, then feel too silly added xbean-spring.

The configuration facet can use the JMS and ACTIVEMQ tags:

Xmlns:amq= "Http://activemq.apache.org/schema/core" xmlns:jms= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JMS"

The corresponding xsi:schemalocation:

Http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsdhttp:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JMS http://www.springframework.org/schema/jms/spring-jms.xsd

For connectionfactory configuration, you can use the AMQ tag:

<id= "Jmsfactory"  brokerurl= "tcp://localhost:61616"/ >

But here I'm going to try Pooledconnectionfactory;
The following explanations are available on the Org.apache.activemq.pool.PooledConnectionFactory website:
If you aren't using a JCA container to manage your JMS connections, we recommend we pooling JMS connection Provi Der, (Org.apache.activemq.pool.PooledConnectionFactory) from the Activemq-pool library, which would pool the JMS resources To work efficiently with Spring's jmstemplate or with EJBs.

For its properties, here are some explanations according to Javadoc:
· Maximumactivesessionperconnection: Maximum session count per connection
· Blockifsessionpoolisfull: The default is session China when the request session is blocked, setting false will throw jmsexception
· MaxConnections: Maximum number of connections
· IdleTimeout: Idle time, default is 30 seconds
· Createconnectiononstartup: Whether to create connection at startup

Here I first use the default parameter declaration, do not know why always reported malformprameterizedtype ...

<bean id= "Jmsfactory" class= "Org.apache.activemq.pool.PooledConnectionFactory"/>
Last used queue, this time swap with topic try ...

<id= "Destination"  class= " Org.apache.activemq.command.ActiveMQTopic ">  <index=" 0 "  value=" Spartatopic "></constructor-arg> </ Bean >

Of course, you can also use AMQ tags:

<physicalname= "Sparta"/>

If you are using queue:

<physicalname= "Sparta"/>

Did I put these in spring just to use labels to facilitate di?
With <spring in Action>, Jmstemplate is <spring part of the core of JMS support >
(another jmsTemplate102 for adaptation to jms1.0.2);
As with JdbcTemplate, Jmstemplate also offers similar advantages.
For example, using Jmstemplate to handle out-of-control JMS code, as JdbcTemplate handles out-of-control JDBC code.
Or, if you use Jmstemplate to capture the exception that Jmsexception,jmstemplate will catch, then throw a spring-brought JMSException subclass exception ( The personal feeling spring provides is not more detailed exception information, but the focus is different ...).

Like what:
· Listenerexecutionfailedexception: Listener Execution failed
· Messageconversionexception: Message conversion failed
· Synchedlocaltransactionfailedexception: Synchronous local transaction not completed
· Uncategorizedjmsexception: No other case for exception
If we catch jmsexception, we can still turn him into jmsexception:

Catch (jmsexception e) {            e.printstacktrace ();             = jmsutils.convertjmsaccessexception (e);        }

Now try configuring the Jmstemplate:

<BeanID= "Jmstemplate"class= "Org.springframework.jms.core.JmsTemplate" >    < Propertyname= "ConnectionFactory" >        <Beanclass= "Org.apache.activemq.pool.PooledConnectionFactory" />    </ Property>    < Propertyname= "Defaultdestination" >        <Amq:topicPhysicalName= "Sparta" />    </ Property></Bean>

This makes it much easier to write code, and all the connectionfactory,connection,session,consumer,producer are gone;
I just need to use template:

New Classpathxmlapplicationcontext ("Classpath:applicationContext.xml"= (jmstemplate) Context.getbean (" Jmstemplate "); Template.send (new  messagecreator () {    publicthrows jmsexception {        = (activemqmapmessage) session.createmapmessage ();        Msg.setstring ("msg", "This is sparta!!" );         return msg;}    });

Only template.receive () is required for reception;

But be careful! This receive is synchronously receiving messages, and he will always block until a message is received.
May think of MessageListener, for example, we can give a Messageconsumer object Setmessagelistener:

Messageconsumer consumer = Session.createconsumer (destination); Consumer.setmessagelistener (New MessageListener () {    publicvoid  onMessage (Message message) {        try  {            System.out.println ("Listener catched:::" +((textmessage) message). GetText ());         Catch (jmsexception e) {            e.printstacktrace ();}}}    );

The MessageListener instance in the above code, if you create a new class to listen to, implement the MessageListener interface and add the Messagedriven annotation there will be a problem-he is not pojo enough. He's intrusive, I don't want any syntax to implement the interface appears in the code.

So I can use Listener-container;
Now I create a class to listen to, for example:

 Public class Customedlistener {    void processhandle (hashmap<string,string> map) {        System.out.println ("msg:::" +map.get ("MSG"));}    }

The message sent by publisher above is Activemqmapmessage, which requires me to define the parameter as the form above.
Then look at how this listener is configured in spring:

class= "Pac.testcase.jms.CustomedListener"/><jms:listener-container connection-factory= " ConnectionFactory ">    <jms:listener destination=" Sparta "ref=" MyListener "method=" ProcessHandle "/>< /jms:listener-container>

So I don't have to call receive, and I get the message.

JMS-ACTIVEMQ Integrated Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.