Maven Dependency
Maven's dependency uses the invisible dependency transitivity, and if it just uses the JMS function to reference maven coordinates,
<dependency> <groupId>org.springframework</groupId> <artifactid>spring-jms</ Artifactid> <version>${project.dependency.spring.core}</version></dependency>
Depending on the transitive relationship, it is visible that the SPRINGJMS will import other dependent packages invisibly
Spring Namespace
Spring-config.xml supports SPRING-JMS namespaces and uses namespace to simplify the configuration of Spring
<?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:jms=" http// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JMS " 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/jms http://www.springframework.org/schema/jms/spring-jms.xsd "> <!-- bean definitions here --></beans>
Spring Beans
The basic need for Spring JMS is to build 2 classes, one JMS Template and one JMS client
JMS Template: is provided by spring itself, simply register the class with the spring container to
JMS client: This needs to be written on its own, and will be used to the JMS template class, if you need spring hosting also need to register in the container
Building the JMS client class
Use the @service annotation to register the class with the spring container and automatically reload the JMS Template with the autowire annotation.
The setter method for defining jmstemplate is primarily to understand decoupling, and it is necessary to set a JMS template instance yourself when departing from the spring container
@Service ("JMSDemo") public class jmsdemo{ private JmsTemplate jmsTemplate; @Autowired public void setjmstempalte (jmstemplate jmstemplate) { this.jmsTemplate = jmsTemplate; } public void send (final string argqueuename, final serializable argobject) throws JMSException { jmstemplate.send (Argqueuename, new messagecreator () { public message createmessage ( Session session) throws JMSException { return session.createobjectmessage (Argobject); } }); } public message consumee (String queuename) throws jmsexception { message message = Jmstemplate.receive (queuename); return message; }}
Spring Config
The spring configuration file first needs Componet-scan to scan the package to register classes with annotations such as @component @Service into the container of spring.
<!--===============================================--><!--Component Scanning--&G t;<!--===============================================--><context:component-scan base-package= "com.*"/ >
Next you need to define the Jms template Bean
The Jms Template requires additional configuration of the ConnectionFactory and Defaultdestination properties Messageconverter is optional, followed by subsequent series.
The spring cacheconnectionfactory is used here to pool the connection.
Finally we need to provide spring with 2 implementation classes, respectively Connectionfacotry and defaultdestination
<!-- =============================================== --><!-- JMS Template --> <!-- =============================================== --><bean id= "JmsTemplate" class= "Org.springframework.jms.core.JmsTemplate" > <property name= " ConnectionFactory " ref=" Cachingconnectionfactory "/> <property name=" Defaultdestination " ref=" Jmsdestination "/> <property name=" Messageconverter "> <bean class=" Org.springframework.jms.support.converter.SimpleMessageConverter "/> </property ></bean><!--&NBSP;POOLED&NBSP;SPRING&NBSP;CONNECTION&NBsp;factory --><bean id= "Cachingconnectionfactory" class= " Org.springframework.jms.connection.CachingConnectionFactory "> <property name = "Targetconnectionfactory" ref= "Jmsconnectionfactory" /></bean>
If you need to go inside Jnid to find a JMS supply, use Jee:jndi-lookup to find it.
The premise is that you need to add Jee's name space
Xmlns:jee= "xsi:schemalocation=" http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/ Jee/spring-jee.xsd "
<jee:jndi-lookup id= "Jmsconnectionfactory" jndi-name= "amqconnectionfactory"/><jee:jndi-lookup id= " Jmsdestination "jndi-name=" Amqdestination "/>
If you need to define the actual class of the implementation class that requires additional definition connectfactory (each vendor may be different), take ACTIVEMQ as an example
<bean id= "Amqconnectionfactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" > <!--brokerurl, you May has different IP or port-to-<property name= "Brokerurl" value= "tcp://localhost:61616"/></bean>< Bean id= "defaultdestination" class= "Org.apache.activemq.command.ActiveMQQueue" > <!--<property name= " Compositedestinations "value=" Testqueue "/>--> <constructor-arg index=" 0 "value=" testQueue "/></bean >
Test
Simple test send message to queue
Preparation requires the introduction of dependent packages ActiveMq and spring-test and the opening of the JMS server
Reference: ActiveMQ Get Started
<dependency> < groupid>org.apache.activemq</groupid> <artifactid>activemq-all</ artifactid> <version>${project.dependency.apache.activemq}</version>< /dependency><dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version >${project.dependency.spring.core}</version> <scope>test</scope> </dependency>
ConnectionFactory connectionfactory = new Activemqconnectionfactory ("tcp://localhost:61616");//Creates an JNDI Context and combine Resourcessimplenamingcontextbuilder builder = Simplenamingcontextbuilder.emptyactivatedcontextbuilder (); Builder.bind ("Amqconnectionfactory", ConnectionFactory) Builder.bind ("Amqdestination", New Activemqqueue ("Testqueue"));//Initialize Spring Contextapplicationcontext context = new Classpathxmlapplicationcontext ("Spring-config.xml"); JMSDemo JMSDemo = Context.getbean (Jmsdemo.class); Jmsdemo.send ("Testqueue", new Object ());
SPINRGFRAMEWORK4 series of SPRINGJMS: (a) build jms-annotations Plus XML version