ActiveMQ and Spring integration (4)

Please reprint from the source: http://eksliang.iteye.com/blog/2242729

1. The package that spring encapsulates JMS must be imported 

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jms</artifactId>
	<version>3.2.9.RELEASE</version>
</dependency>

<dependency>
	 <groupId>org.apache.activemq</groupId>
	 <artifactId>activemq-all</artifactId>
	 <version>5.5.0</version>
</dependency>
<!-- ActiveMQ dependent log package-->
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.14</version>
</dependency>

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.5.6</version>
</dependency>

 

2. Create a connection factory

Configure the JMS connection factory to let Spring know how to connect to ActiveMQ. ActiveMQConnectionFactory is a connection factory that comes with ActiveMQ, which can be configured in spring as follows:

<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
	<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>

 

3. Declare ActiveMQ message destination

The message destination can be a queue or a topic, depending on the needs of the application. If it is the queue destination and topic destination created separately in spring

<!-- The purpose of the queue -->
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
	<constructor-arg value="queue.destination"/>
</bean>

<!-- the destination of the theme -->
<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
	<constructor-arg value="topic.Destination"/>
</bean>

 

 Fourth, use Spring's template code to simplify the sending and receiving of messages

For eliminating redundant and duplicated JMS code, Spring's solution is JmsTemplate. In order to use JmsTemplate, we need to declare it as a Bean in Spring's configuration file, as shown below.

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
	<property name="connectionFactory" ref="connectionFactory"/>
	<!-- The following two are not necessary, because the method of jmsTemplate can specify the destination -->
	<!-- <property name="defaultDestinationName" value="queue.destination"/> -->
	<property name="defaultDestination" ref="queue"></property>
</bean>
	
<!-- Assembly send message entity-->
<bean id="queueSendService" class="com.gosun.jms.QueueSendService">
	<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>
<!-- Assemble the receiving message entity-->
<bean id="queuesAcceptService" class="com.gosun.jms.QueuesAcceptService">
	<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>

 

  • The code of com.gosun.jms.domain.User is as follows:
package com.gosun.jms.domain;

import java.io.Serializable;

public class User implements Serializable{
	private static final long serialVersionUID = 13531L;
	private String userName;
	private String userPwd;
	private Float  sal;
	
	get() set().....! Omit
	
	@Override
	public String toString() {
		return "User [userName=" + userName + ", userPwd=" + userPwd + ", sal="
				+ sal + "]";
	}
}
  •  com.gosun.jms.QueueSendService code
package com.gosun.jms;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.gosun.jms.domain.User;

/**
 * Send message, the producer of the message
 * @author Ickes
 */
public class QueueSendService {
	
	private JmsTemplate jmsTemplate;
	
	/**
	 * Send serialized messages using JmsTemplate
	 * @param user
	 */
	public void send(final User user){
// jmsTemplate.send(destination, messageCreator)//You can specify the destination yourself
		jmsTemplate.send(new MessageCreator() {
			@Override
			public ObjectMessage createMessage(Session session) throws JMSException {
				ObjectMessage msg = session.createObjectMessage();
				msg.setObject(user);
				return msg;
			}
		});
	}

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
}

 

  • The code of com.gosun.jms.QueuesAcceptService is as follows
package com.gosun.jms;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import org.springframework.jms.core.JmsTemplate;

/**
 * Receive messages, consumers of messages
 * @author Ickes
 */
public class QueuesAcceptService {
	private JmsTemplate jmsTemplate;
	
	/**
	 * receive messages
	 */
	public void receive(){
		ObjectMessage msg = (ObjectMessage) jmsTemplate.receive();
		try {
			System.out.println(msg.getObject());
		} catch (JMSException e) {
			e.printStackTrace ();
		}
	}

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
}

 

The test code is as follows:

public static void main(String[] args) {
		ApplicationContext context =new  ClassPathXmlApplicationContext("applicationContext-activeMQ.xml");
		// send the message first
		QueueSendService qss = (QueueSendService) context.getBean("queueSendService");
		User user = new User();
		user.setUserName("ickes");
		user.setUserPwd("123456");
		user.setSal(12000F);
		qss.send(user);
		// receive message
		QueuesAcceptService qas = (QueuesAcceptService) context.getBean("queuesAcceptService");
		qas.receive();

 

When calling the receive() method of JmsTemplate, JmsTemplate will try to get a message from ActiveMQ. If no message is available, the receive() method waits until a message is obtained. That is, the method is synchronous. This means that the receiver has to wait patiently for the message to arrive, so the receive() method will block until there is a message available (or the message times out). Does it feel weird to receive messages sent asynchronously synchronously? How to solve this problem?

Answer: Using message-driven POJO, how to use it, will be explained in the next blog.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267758&siteId=291194637