Using the message middleware in the previous section to learn notes six –spring the JMS theory and know how spring integrates JMS, we will learn how to use JMS in spring in this section. Create a MAVEN project with idea Import Dependencies
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "HTT" P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apach E.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.winter.jms</ groupid> <artifactId>spring-jms</artifactId> <version>1.0-SNAPSHOT</version> <p roperties> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependen cies> <dependency> <groupId>junit</groupId> <ARTIFACTID>JUNIT&L T;/artifactid> <version>4.11</version> <scope>test</scope> < /dependency> <dependency> <groupId>org.springframework</groupId> < Artifactid>spring-conteXt</artifactid> <version>${spring.version}</version> </dependency> ;d ependency> <groupId>org.springframework</groupId> <artifactid>spring-jms< ;/artifactid> <version>${spring.version}</version> </dependency> <depe Ndency> <groupId>org.springframework</groupId> <artifactid>spring-test</ar tifactid> <version>${spring.version}</version> </dependency> <dependen Cy> <groupId>org.apache.activemq</groupId> <artifactid>activemq-core& Lt;/artifactid> <version>5.7.0</version> <exclusions> ;exclusion><!--because we have already quoted this package, and this package is included in this package, we need to eliminate--> <artifactId>spring-context<
/artifactid> <groupId>org.springframework</groupId> </exclusion> </exclusi
ons> </dependency> </dependencies> </project>
Project Structure
Create configuration Spring configuration: Producer.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/sch Ema/beans/spring-beans.xsd Http://www.springframework.org/schema/context H Ttp://www.springframework.org/schema/context/spring-context.xsd "> <!--open annotation support--> <context:annotatio N-config/> <!--activemq provide us with connectionfactory--> <bean id= "Targetconnectionfactory" class=
Che.activemq.ActiveMQConnectionFactory "> <property name=" brokerurl "value=" tcp://127.0.0.1:61616 "/>" </bean> <!--Spring JMS provides us with the connection pool--> <bean id= "ConnectionFactory class=" org.springframework.jms.c Onnection. SingleconnectiOnfactory "> <property name=" targetconnectionfactory "ref=" Targetconnectionfactory "/> </bean>
<!--the destination of a queue, point to point--> <bean id= "queuedestination" class= "Org.apache.activemq.command.ActiveMQQueue" > <constructor-arg value= "queue"/> </bean> <!--configuration jmstemplate, for sending messages--> <bean "JMS Template "class=" org.springframework.jms.core.JmsTemplate "> <property name=" connectionfactory "ref=" connecti Onfactory "/> </bean> <bean class=" Com.winter.jms.producer.impl.ProducerServiceImpl "/> </beans& Gt
Queue Mode
Create a producer service
Interface
Package com.winter.jms.producer;
/**
*
* Created by the Administrator on 2017/11/2.
*
/public interface Producerservice {
void SendMessage (String message);
}
Implement
package Com.winter.jms.producer.impl;
Import Com.winter.jms.producer.ProducerService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.jms.core.JmsTemplate;
Import Org.springframework.jms.core.MessageCreator;
Import Javax.annotation.Resource;
Import javax.jms.*;
/** * * Created by the Administrator on 2017/11/2.
* * Public class Producerserviceimpl implements Producerservice {@Autowired jmstemplate jmstemplate;
@Resource (name = "queuedestination") destination destination; public void SendMessage (final String message) {jmstemplate.send (destination, new Messagecreator () {p Ublic message CreateMessage throws jmsexception {TextMessage TextMessage = session.creat
Etextmessage (message);
SYSTEM.OUT.PRINTLN ("Send message = [" + textmessage.gettext () + "]");
return textmessage;
}
}); }
}
Start class
Package com.winter.jms.producer;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Producer Service startup class
* Created by Administrator on 2017/11/2.
*
/public class Appproducer {public
static void Main (string[] args) {
classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Producer.xml");
Producerservice service = Context.getbean (producerservice.class);
for (int i = 0; i < i++) {
service.sendmessage ("test" + i);
}
Context.close ();
}
TestOpen ACTIVEMQ Background: http://localhost:8161/start the "main" method in the Producer startup class that you just created
You can see the normal output. Create consumer Services
To create a new package: consumer
Now look at the structure of the project:
Create a new spring configuration file: Consumer.xml
Because some of the configuration two profiles have a common part, we have added a new profile: Common.xml is used to store the same configuration in two configuration files. Take a look at the contents of the three configuration files after the change.
Common.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" 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 "> <!--open Annotation support--> <context:annotation-config/> <!--ACTIVEMQ offers us con Nectionfactory--> <bean id= "targetconnectionfactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" > <property name= "brokerurl" value= "tcp://127.0.0.1:61616"/> </bean> <!--spring JMS for me We provide the connection pool--> <bean id= "ConnectionFactory" class= "Org.springframework.jms.connection.SingleConnectionFactory" > <property name= "TargetconNectionfactory "ref=" Targetconnectionfactory "/> </bean> <!--The destination of a queue, point to point--> <bean" id= " Edestination "class=" Org.apache.activemq.command.ActiveMQQueue "> <constructor-arg value=" queue "/> ;/bean> </beans>
Producer.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"Http://www.springframework.org/schema/beans"
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 ">
<!--Import public configuration-->
<import resource= "Common.xml"/>
<!--configuration jmstemplate, for sending messages-->
<bean id= "Jmstemplate" class= "org.springframework.jms.core.JmsTemplate" >
<property name= " ConnectionFactory "ref=" ConnectionFactory "/>
</bean>
<bean class=" Com.winter.jms.producer.impl.ProducerServiceImpl "/>
</beans>
Consumer.xml
<?xml version= "1.0" encoding= "UTF-8"?> "<beans xmlns=" http:// Www.springframework.org/schema/beans "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "Xsi:schemalocati on= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!--Import public configuration--> <import resource= "Common.xml"/> <!--Configure message listener--> <bean "con Sumermessagelistener "class=" Com.winter.jms.consumer.ConsumerMessageListener/> <!--Configure message listening container--> <bea n id= "Jmscontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" > <property name = "ConnectionFactory" ref= "ConnectionFactory"/> <!--message Listener address--> <property name= "Destination" re f= "Queuedestination"/> <!--message listener--> <property name= "MessageListener" ref= Tener "/> </bean> </beans>
Consumer
Consumer Service Listener
Consumermessagelistener
Package Com.winter.jms.consumer;
Import javax.jms.JMSException;
Import Javax.jms.Message;
Import Javax.jms.MessageListener;
Import Javax.jms.TextMessage;
/**
* Message Listener
* Created by the Administrator on 2017/11/2.
*
/public class Consumermessagelistener implements MessageListener {public
void OnMessage C11/>textmessage TextMessage = (textmessage) message;
try {
System.out.println ("Receive message:" + textmessage.gettext ());
} catch (JMSException e) {
e.printstacktrace ();}}}
Consumer Listener Startup Class
Appconsumer
Package Com.winter.jms.consumer;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Consumer Startup class
* Created by Administrator on 2017/11/2.
*/Public
class Appconsumer {public
static void Main (string[] args) {
ApplicationContext context = new CLASSP Athxmlapplicationcontext ("Consumer.xml");
}
Test
Start the "main" method to see the message is consumed, see the activemq background can see. Since I started the producer two times before, resulting in the production of 200 messages, has now been consumed by all
What would happen if we started two consumers at the same time?
Then we start a producer again.
You can see the information that the console prints:
Consumer A (Appconsumer):
Receive MESSAGE:TEST0 receive
MESSAGE:TEST2 receive MESSAGE:TEST4 receive MESSAGE:TEST6 receive
message:test8
Receive message: Test10 receive
message:test12 receive message:test14 receive message:test16 receive
message:test18
Receive message: TEST20 receive
message:test22 receive
message:test24 receive
message:test26
...
Consumer B (Appconsumer):
Receive Message:test1 receive
MESSAGE:TEST3 receive MESSAGE:TEST5 receive MESSAGE:TEST7 receive
message:test9
Receive MESSAGE:TEST11 receive
message:test13 receive message:test15 receive message:test17 receive
message:test19
Receive message:test21 receive
message:test23 receive
message:test25 receive
message:test27
...
It can be seen that the queue mode is the average consumption, consumer a consumption of the message can no longer be consumer B consumption Theme Mode Configure to add in Common.xml:
<!--a theme destination, publish subscription mode-->
<bean id= "topicdestination" class= "Org.apache.activemq.command.ActiveMQTopic" ">
<constructor-arg value=" topic "/>
</bean>
To Producerserviceimpl in the following:
@Resource (name = "Queuedestination")
destination destination;
Change into
@Resource (name = "Topicdestination")
destination destination;
Modify Consumer.xml:
Modify
<!--message Listener address-->
<property name= "Destination" ref= "Queuedestination"/>
Modified into:
<!--message Listener address-->
<property name= "Destination" ref= "Topicdestination"/>
It takes only three steps to change the queue mode to theme mode . Test:
Start two consumer Appconsumer, then start a producer Producer can see the output:
Consumer A:
receive MESSAGE:TEST0 receive MESSAGE:TEST1 receive MESSAGE:TEST2 receive MESSAGE:TEST3 Receive message: TEST5 receive MESSAGE:TEST6 receive MESSAGE:TEST7 receive MESSAGE:TEST8 receive MESSAGE:TEST9 receive message:test10 receive MESSAGE:TEST11 reception message:t Est12 Receive