Spring+ActiveMQ JMS實現

第一步:

首先要下載activeMQ :

http://activemq.apache.org/download.html

這裡以windows版本為例,下载后解压得到: 

Spring+ActiveMQ JMS實現_第1张图片

双击bin目录下activemq.bat启动activemq服务,和tomcat一样有简单管理页面:

http://localhost:8161/admin/ 在这里可以连接消息队列等进行管理 

ok,第一步完成


第二步:

spring中配置

先将以下包加入到Classpath中:

activemq-all-5.4.3.jar

geronimo-jta_1.0.1B_spec-1.0.1.jar、geronimo-jms_1.1_spec-1.1.1.jar、geronimo-j2ee-management_1.1_spec-1.0.1.jar、activemq-core-5.4.3.jar

当然还有spring jms包

配置连接工厂(这里使用连接池-activemq提供)、destination(activemq提供)及JmsTemplate(spring提供的消息发送模板):

	<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
		destroy-method="stop">
		<property name="connectionFactory">
			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
				<property name="brokerURL" value="${jms.activemq.brokerURL}" />
				<property name="clientID" value="${jms.activemq.clientID}"></property>
			</bean>
		</property>
	</bean>

	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="${jms.activemq.queue.name}" />
	</bean>

	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory">
			<ref bean="connectionFactory" />
		</property>
	</bean>

上面property配置:

jms.activemq.brokerURL=tcp://yourip:61616
jms.activemq.clientID=jms-gds
jms.activemq.queue.name=GDS-RED5-Queue

然后消息发送:

	<bean id="messageProducer" class=xx.jms.gdsred5.gds.MsgProducer">
		<property name="template" ref="jmsTemplate" />
		<property name="destination" ref="destination" />
	</bean>

MsgProducer.java

package xx.jms.gdsred5.gds;

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

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

public class MsgProducer {

	private JmsTemplate template;

	private Destination destination;

	public void send(final String msg) {
		template.send(destination, new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				TextMessage message = session.createTextMessage();
				//設置消息類型
				message.setJMSType("test");
				message.setText(msg);

				return message;
			}


		});

	}

	public void setDestination(Destination destination) {
		this.destination = destination;
	}

	public void setTemplate(JmsTemplate template) {
		this.template = template;
	}
}

消息接收配置:

	<bean id="messageListener" class="xx.jms.gdsred5.red5.MsgListener">
	</bean>

	<bean id="listenerContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="destination" />
		<property name="messageListener" ref="messageListener" />
	</bean>

MsgListener.java

package xx.jms.gdsred5.red5;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


public class MsgListener implements MessageListener {

	private IMsgProcessor msgProcessor;

	@Override
	public void onMessage(Message message) {
		try {
			//消息應該是TextMessage
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				String message = textMessage.getText();
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}

}


ok. 收工


你可能感兴趣的:(Spring+ActiveMQ JMS實現)