穷水叮咚的博客

主要针对,学习和工作中发现的问题,进行记录和整理,然后和大家分享讨论。...

SSM+activemq结合

SSM搭建很简单,就不说了。

直接添加activemq

在pom里添加依赖

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.jms</groupId>
			<artifactId>jms</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
			<version>5.9.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.xbean</groupId>
			<artifactId>xbean-spring</artifactId>
			<version>3.16</version>
		</dependency>

新建active.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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-4.1.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-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.9.0.xsd">
 	
 	<!-- ActiveMQ 连接工厂 -->
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <amq:connectionFactory id="amqConnectionFactoryBOSS" brokerURL="tcp://xxx.xxx.xxx.xxx:61616" userName="" password="" />
    
    <!-- Spring Caching连接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> 
    <bean id="connectionFactoryBoss" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="amqConnectionFactoryBOSS" />
         <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="100" />
    </bean>
    
    <!-- ======== queue size config start ======== -->
    <amq:systemUsage sendFailIfNoSpaceAfterTimeout="3000" sendFailIfNoSpace="true">
	    <amq:memoryUsage>
	    	<amq:memoryUsage limit="20 mb" ></amq:memoryUsage>
	    </amq:memoryUsage> 
	    <amq:storeUsage>
	    	<amq:storeUsage limit="200 mb"></amq:storeUsage>
	    </amq:storeUsage>
	    <amq:tempUsage>
	    	<amq:tempUsage limit="100 mb"></amq:tempUsage>
	    </amq:tempUsage>
    </amq:systemUsage>
     
    
    <bean id="jmsQueueTemplateBoss" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactoryBoss" />
        <property name="pubSubDomain" value="false" />
    </bean>
    
    <!-- ======== 监听status======== -->
    <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactoryBoss" acknowledge="auto">
        <jms:listener destination="status" ref="queueReceiverCardStatus"/>
    </jms:listener-container>
    <!-- ======== Consumer side end ======== -->
 
</beans>

新建 QueueSenderBoss.java

package com.pwl.cash;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class QueueSenderBoss {
	
	@Autowired
	@Qualifier("jmsQueueTemplateBoss")
	private JmsTemplate jmsTemplate;
	
	public void send(String queueName, final String message) {
		jmsTemplate.send(queueName, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});
	}
	
}

新建

QueueReceiverCardStatus.java
package com.pwl.cash;


import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;

@Component
public class QueueReceiverCardStatus implements MessageListener {
	
	
	@Override
	public void onMessage(Message message) {
		try {
			String reqJsonStr = ((TextMessage)message).getText();
			System.out.println(reqJsonStr);
		}catch (JMSException e) {
			e.printStackTrace();
		} 
	}
	
}

测试

package com.pwl.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.pwl.cash.QueueSenderBoss;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试  
@ContextConfiguration(locations={"classpath:spring-servlet.xml","classpath:spring-redis.xml","classpath:active.xml"}) //加载配置文件
public class TestRedis {
	@Resource
	private QueueSenderBoss queueSenderBoss;
	
	@Test
	public void ttt() {
		queueSenderBoss.send("status","stop");
	}
}

结果


阅读更多
个人分类: activemq
上一篇CentOS7安装Tomcat
下一篇ActiveMQ处理失败时的消息重发机制
想对作者说点什么? 我来说一句

Xsl与xml结合Xsl与xml结合

2011年08月15日 192KB 下载

没有更多推荐了,返回首页

关闭
关闭