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">
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<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">
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
消费者可以配置多个 ,
<bean id="消息监听器实现类" class="cn.lwb.MyMessageListener"></bean>
<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) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-activemq.xml");
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
Destination destination = context.getBean(Destination.class);
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) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}