spring-integrated ActiveMQ

Subscribe-listener-message

  • General consumers and listeners are configured at the same time
  • Consumers can configure multiple, that means listeners, also configure multiple
	
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.37.161:61616" />
	</bean>
	
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
	<!-- Spring提供的JMS工具类 -->
	<!--它可以进行消息发送 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
 
 


	<!--这个是主题目的地 -->
	<!--订阅 -->
	 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> 
	     <constructor-arg value="topic" /> 
	 </bean>

Point-to-point-listener-message

  • This is a peer-to-peer configuration, if you need peer-to-peer to directly replace the above subscription.
<!--这个是队列目的地 -->
<!--点对点 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
	<constructor-arg>
		<value>queue</value>
	</constructor-arg>
</bean>

Consumer-receive message-configuration

  • Consumers can configure multiple, the same, the listener can also be configured multiple, and also need to configure the corresponding factory class
    • ActiveMQConnectionFactory ,
    • SingleConnectionFactory ,
  • The type of message sent, whether it is point-to-point or subscribed. If you forget to search on the current page, you can find it
  • Configure subscription or point-to-point, depending on whether the information sent is point-to-point or subscription
  • Configure these two to receive messages
 
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.37.161:61616" />
	</bean>
	
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>
	<!-- Spring提供的JMS工具类 -->
	<!--它可以进行消息发送 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>
 


	<!--这个是主题目的地 -->
	<!--订阅 -->
	 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> 
	     <constructor-arg value="topic" /> 
	 </bean>


    <!---------------------------------------------------------------------------------------------------->
         消费者可以配置多个 ,
    <!-- messageListener实现类-自己编写的实现类 -->
	<bean id="消息监听器实现类" class="cn.lwb.MyMessageListener"></bean>
	<!-- 配置一个jsm监听容器 -->
	<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="配置的是订阅/点对点" />
		<property name="messageListener" ref="配置消息监听器实现类" />
	</bean>
    
	<bean id="myMessageListener2" class="cn.lwb.MyMessageListener"></bean>
	<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="queueDestination" />
		<property name="messageListener" ref="myMessageListener2" />
	</bean>

JAVA-message sending

  • For example, if the spring container does not need to be obtained,
  • And the container's beans, only need to be injected
public static void main(String[] args) {
// 创建spring容器
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-activemq.xml");

		// 从spring容器中获取JMSTemplate,这个对象是用于发送消息的
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

		// 从spring容器中获取Destination对象
		Destination destination = context.getBean(Destination.class);

		// 使用JMSTemplate发送消息
		jmsTemplate.send(destination, new MessageCreator() {

			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = new ActiveMQTextMessage();
				textMessage.setText("发送的消息12312312");
				System.out.println("开始发消息了");

				return textMessage;
			}
		});

JAVA-Message Reception

  • To achieve message reception needs to implement an excuse
public class MyMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		// 判断消息类型是TextMessage
		if (message instanceof TextMessage) {
			// 如果是,则进行强转
			TextMessage textMessage = (TextMessage) message;
			try {
				// 8. 消费消息,打印消息内容
				String text = textMessage.getText();
				System.out.println(text);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
Published 20 original articles · Likes0 · Visits 930

Guess you like

Origin blog.csdn.net/vistaed/article/details/105558918