spring-整合ActiveMQ

订阅-监听器-发消息

  • 一般消费者和监听器同时配置
  • 消费者可以配置多个,那就意味着监听器,也是配置多个
	
	<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>

点对点-监听器-发消息

  • 这个是点对点配置 , 如果需要点对点直接替换上面的订阅即可.
<!--这个是队列目的地 -->
<!--点对点 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
	<constructor-arg>
		<value>queue</value>
	</constructor-arg>
</bean>

消费者-收消息-配置

  • 消费者可以配置多个,那同样的,监听器也可以配置多个,并且也得需要配置对应的工厂类
    • ActiveMQConnectionFactory ,
    • SingleConnectionFactory ,
  • 发送消息的类型,是点对点还是订阅如果忘记了在当前页面搜索即可找到
  • 配置订阅还是点对点,取决于发送信息是点对点,还是订阅
  • 配置这两个主要用于接收消息
 
	<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-消息发送

  • 比如,想spring容器并不需要获取,
  • 以及容器的bean,只需要注入即可
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-消息接收

  • 实现消息接收需要实现一个借口
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();
			}
		}
	}
}
发布了20 篇原创文章 · 获赞 0 · 访问量 930

猜你喜欢

转载自blog.csdn.net/vistaed/article/details/105558918
今日推荐