ActiveMQ v 5.8.0 과 Spring v 3.1.4 통합

4433 단어 activemq
Spring 3.1.4 와 통합:
1:
다운로드:http://mirror.bit.edu.cn/apache/activemq/apache-activemq/5.8.0/apache-activemq-5.8.0-bin.zip
2:
압축 해제 후: activemq-client-5.8.0.jar,geronimo-jms_1.1_spec-1.1.1.jar,geronimo-j2ee-management_1.1_프로젝트 구축 경로 lib 에 spec-1.0.1.jar 넣 기
 
3:
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
	default-lazy-init="true">
	<description>ActiveMQ      </description>

	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL">
			<value>tcp://192.168.6.253:61616</value>
		</property>
	</bean>
	
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory">
			<ref local="connectionFactory"/>
		</property>
	</bean>
	<bean id="jmsDestination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0">
			<value>jmsDestination</value>
		</constructor-arg>
	</bean>

</beans>

 
4:유닛 테스트 코드:
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.test.context.ContextConfiguration;

import com.capitalbio.soft.test.core.SpringTransactionalTestCase;

@ContextConfiguration(locations = { "/applicationContext.xml","/applicationContext-activemq.xml" })
public class JmsTest extends SpringTransactionalTestCase {
	
	@Autowired private JmsTemplate jmsTemplate;
	@Resource(name="jmsDestination") private Destination jmsDestination;

	@Test
	public void testJmsSend() {
		final ConcurrentLinkedQueue<String> coll = new ConcurrentLinkedQueue<String>();
		for(int i=0;i<10;i++){
			final int j = i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					for(int a=0;a<10;a++){
						String msg = "JMS      ..." + a + j + Thread.currentThread().getName();
						jmsTemplate.convertAndSend(jmsDestination, msg);
//						jmsTemplate.convertAndSend("dest", msg);
						coll.add(msg);
						System.out.println(msg);
					}
				}
			}).start();
		}
		try {
			Thread.sleep(9000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(coll.size());
	}
	
	@Test
	public void testJmsReceive() {
		final ConcurrentLinkedQueue<String> coll = new ConcurrentLinkedQueue<String>();
		for(int i=0;i<10;i++){
			new Thread(new Runnable() {
				@Override
				public void run() {
					for(int a=0;a<111;a++){
						String msg = (String)jmsTemplate.receiveAndConvert(jmsDestination);
//						String msg = (String)jmsTemplate.receiveAndConvert("abc");
						coll.add(msg);
						System.out.println(msg);
					}
				}
			}).start();
		}
		try {
			Thread.sleep(4000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(coll.size());
	}
	
}

 
5:유닛 테스트 시작,테스트 성공!

좋은 웹페이지 즐겨찾기