1. 程式人生 > >以SpringMVC框架為中心瘋狂擴充套件-05、新增ActiveMQ依賴

以SpringMVC框架為中心瘋狂擴充套件-05、新增ActiveMQ依賴

1、新增ActiveMQ需要的一些依賴

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.7.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>4.3.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.xbean</groupId>
			<artifactId>xbean-spring</artifactId>
			<version>4.5</version>
		</dependency>
2、新進一個ActiveMQ的spring配置檔案並import到主配置檔案中
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:amq="http://activemq.apache.org/schema/core"
	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-4.1.xsd
     	http://www.springframework.org/schema/mvc 
     	http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
     	http://www.springframework.org/schema/jms
     	http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
     	http://activemq.apache.org/schema/core
     	http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
     	
	<amq:connectionFactory id="amqConnectionFactory"
		brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin" />
	<!-- 配置connectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.CachingConnectionFactory">
		<!-- 目標ConnectionFactory 引用至 amqConnectionFactory-->
		<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
		<!-- Session快取數量 -->
		<property name="sessionCacheSize" value="100" />
		
	</bean>
	
	<!-- Spring JmsTemplate 的訊息生產者 -->

    <!-- 定義JmsTemplate的Queue型別 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 引用自定義的 ConnectionFactory物件  -->  
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(釋出/訂閱),即佇列模式 -->
        <property name="pubSubDomain" value="false" />
        <!-- 預設目標名稱(佇列名) -->
        <property name="defaultDestinationName" value="syxQueue" />
    </bean>

    <!-- 定義JmsTemplate的Topic型別 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 引用自定義的 ConnectionFactory物件 -->  
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(釋出/訂閱) -->
        <property name="pubSubDomain" value="true" />
        <!-- 預設目標名稱(訂閱名) -->
        <property name="defaultDestinationName" value="syxTopic" />
    </bean>

	<bean id="messageSender" class="com.syx.producter.sender.MessageSender">
        <property name="jmsTopicTemplate" ref="jmsTopicTemplate"></property>

    </bean>
</beans>

3、建立訊息傳送類和Controller類

package com.syx.producter.sender;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Repository;

/** 
 * 訊息傳送者
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
@Repository
public class MessageSender {
	
	private JmsTemplate jmsTopicTemplate;
	
	public void sendInfo(final String info){
		jmsTopicTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                final MapMessage message = session.createMapMessage();
                message.setString("info", info);
                return message;
            }

        });
	}

	public JmsTemplate getJmsTopicTemplate() {
		return jmsTopicTemplate;
	}

	public void setJmsTopicTemplate(JmsTemplate jmsTopicTemplate) {
		this.jmsTopicTemplate = jmsTopicTemplate;
	}
}
package com.syx.producter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.syx.producter.sender.MessageSender;

/**
 * 訊息傳送者Controller類
 *
 * @author sunyx
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/sender")
public class SenderController {
	@Autowired
	private MessageSender messageSender;

	@RequestMapping("/senderinfo")
	public String senderinfo() {
		final String info = "hello syx revicer";
		System.out.println("sender:" + info);
		messageSender.sendInfo("hello syx revicer");
		return "syx";
	}

}
4、啟動ActiveMQ和tomcat,在activrMQ的Topic就可以看到傳送過去的訂閱資訊了。


5、配置監聽者

在spring-activemq.xml中新增一個Receiver類並注入jmsTopicTemplate,讓一個controller注入Receiver類並呼叫接收方法。

<bean id="messageReceiver" class="com.syx.customer.receiver.MessageReceiver">
        <property name="jmsTopicTemplate" ref="jmsTopicTemplate"></property>

</bean>
package com.syx.customer.receiver;

import javax.jms.JMSException;
import javax.jms.MapMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Repository;

/** 
 * 訊息接收類
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
@Repository
public class MessageReceiver {
	
	private JmsTemplate jmsTopicTemplate;
	
	public String recive() throws JMSException {
		String my = "";
        final MapMessage message = (MapMessage) jmsTopicTemplate.receive();
        my = message.getString("info");
        return my;
    }

	public JmsTemplate getJmsTopicTemplate() {
		return jmsTopicTemplate;
	}

	public void setJmsTopicTemplate(JmsTemplate jmsTopicTemplate) {
		this.jmsTopicTemplate = jmsTopicTemplate;
	}
}

6、啟動jtomcat就可以接收到資訊了。