我们使用jms一般是使用spring-jms和activemq相结合,通过spring的JmsTemplate发送消息到指定的Destination。
- <bean
id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" -
destroy-method="stop"> -
<property name="connectionFactory"> -
<bean class="org.apache.activemq.ActiveMQConnectionFactor y"> -
<property name="brokerURL" -
value="failover:(tcp://192.168.20.23:61616?wireFormat.maxInactivityDuration=0)&maxReconnectDelay=1000" /> -
</bean> -
</property> -
<property name="maxConnections" value="1"></property> - </bean>
定义jmsTempalte的实例:
- <bean
id="oamTmpTopic" class="org.apache.activemq.command.ActiveMQTopic"> -
<constructor-arg value="oamTmpTopic" /> - </bean>
-
- <bean
id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> -
<property name="connectionFactory" ref="connectionFactory" /> -
<property name="defaultDestination" ref="oamTmpTopic" /> -
<property name="explicitQosEnabled" value="true" /> -
<property name="deliveryMode" value="1" /> - </bean>
- import
javax.jms.JMSException; - import
javax.jms.Message; - import
javax.jms.Session; - import
javax.jms.TextMessage; - import
javax.jms.Topic; -
- import
org.springframework.jms.core.JmsTemplate; - import
org.springframework.jms.core.MessageCreator; -
- public
class SendMessage { -
-
private JmsTemplate jmsTemplate; -
-
private String topicName; -
-
private Topic topic; -
-
public void setJmsTemplate(JmsTemplate jmsTemplate) { -
this.jmsTemplate = jmsTemplate; -
} -
-
public void setTopicName(String topicName) { -
this.topicName = topicName; -
} -
-
public void sendMessage(final String message) { -
-
try { -
if (topic == null) { -
topic = jmsTemplate.getConnectionFactory().createConnection() -
.createSession(false, Session.AUTO_ACKNOWLEDGE) -
.createTopic(topicName); -
} -
jmsTemplate.send(topic,new MessageCreator() { -
-
@Override -
public Message createMessage(Session session) -
throws JMSException { -
-
TextMessage textMessage = session -
.createTextMessage(message); -
return textMessage; -
} -
}); -
} catch (JMSException e) { -
e.printStackTrace(); -
} -
} - }
定义消费者TestListener.java:
- import
javax.jms.JMSException; - import
javax.jms.Message; - import
javax.jms.MessageListener; - import
javax.jms.Session; - import
javax.jms.Topic; -
- import
org.springframework.jms.core.JmsTemplate; - import
org.springframework.jms.listener.DefaultMessageListenerCo ntainer; -
- public
class TestListener implements MessageListener{ -
-
private JmsTemplate jmsTemplate; -
-
private String topicName; -
-
public TestListener(JmsTemplate jmsTemplate,String topicName){ -
-
this.jmsTemplate = jmsTemplate; -
-
this.topicName = topicName; -
-
Topic topic; -
try { -
topic = this.jmsTemplate.getConnectionFactory().createConnection().createSession(false, -
Session.AUTO_ACKNOWLEDGE).createTopic(this.topicName); -
-
DefaultMessageListenerCo ntainer dmc = new DefaultMessageListenerCo ntainer(); -
dmc.setPubSubDomain(true); -
dmc.setDestination(topic); -
dmc.setConnectionFactory(this.jmsTemplate.getConnectionFactory()); -
dmc.setPubSubNoLocal(true); -
dmc.setMessageListener(this); -
dmc.setSessionAcknowledgeMod e(Session.AUTO_ACKNOWLEDGE); -
dmc.initialize(); -
dmc.start(); -
} catch (JMSException e) { -
e.printStackTrace(); -
} -
} -
-
@Override -
public void onMessage(Message message) { -
-
System.out.println(message); -
} -
- }
- <bean
id="testListener" class="net.kentop.test.jms.TestListener"> - <constructor-arg
ref="jmsTemplate"></constructor-arg> - <constructor-arg
value="testTopic"></constructor-arg> - </bean>
-
- <bean
id="sendMessage" class="net.kentop.test.jms.SendMessage"> - <property
name="jmsTemplate" ref="jmsTemplate"></property> - <property
name="topicName" value="testTopic"></property> - </bean>
- import
org.springframework.context.ApplicationContext; - import
org.springframework.context.support.ClassPathXmlApplicationC ontext; -
- public
class BeanTest { -
-
public static ApplicationContext context = new ClassPathXmlApplicationC ontext("infrastructure-config.xml"); -
-
public static void main(String args[]){ -
-
SendMessage sendMessage = (SendMessage) context.getBean("sendMessage"); -
-
sendMessage.sendMessage("hahahha,我来测试了"); -
sendMessage.sendMessage("dfsdfsfsdfsdfsdf"); -
sendMessage.sendMessage("come on baby!"); -
sendMessage.sendMessage("hahahha,我来测试了2"); -
sendMessage.sendMessage("dfsdfsfsdfsdfsdf2"); -
sendMessage.sendMessage("come on baby!2"); -
sendMessage.sendMessage("hahahha,我来测试了3"); -
sendMessage.sendMessage("dfsdfsfsdfsdfsdf3"); -
sendMessage.sendMessage("come on baby!3"); -
sendMessage.sendMessage("hahahha,我来测试了4"); -
sendMessage.sendMessage("dfsdfsfsdfsdfsdf4"); -
sendMessage.sendMessage("come on baby!4"); -
} - }
- DefaultMessageListenerCo
ntainer dmc = new DefaultMessageListenerCo ntainer(); - dmc.setPubSubDomain(true);
- dmc.setDestination(topic);
- dmc.setConnectionFactory(this.jmsTemplate2.getConnectionFactory());
-
dmc.setPubSubNoLocal(true); -
dmc.setMessageListener(this); - dmc.setSessionAcknowledgeMod
e(Session.AUTO_ACKNOWLEDGE); - dmc.initialize();
- dmc.start();
dmc.setPubSubDomain(true);
- <bean
id="connectionFactory2" class="org.apache.activemq.pool.PooledConnectionFactory" - destroy-method="stop">
- <property
name="connectionFactory"> -
<bean class="org.apache.activemq.ActiveMQConnectionFactor y"> -
<property name="brokerURL" -
value="failover:(tcp://192.168.20.23:61616?wireFormat.maxInactivityDuration=0)&maxReconnectDelay=1000" /> -
</bean> - </property>
- <property
name="maxConnections" value="1"></property> - lt;/bean>
- <bean
id="jmsTemplate2" class="org.springframework.jms.core.JmsTemplate"> - <property
name="connectionFactory" ref="connectionFactory2" /> - <property
name="defaultDestination" ref="oamTmpTopic" /> - <property
name="explicitQosEnabled" value="true" /> - <property
name="deliveryMode" value="1" /> - lt;/bean>
- import
javax.jms.JMSException; - import
javax.jms.Message; - import
javax.jms.MessageListener; - import
javax.jms.Session; - import
javax.jms.Topic; -
- import
org.springframework.jms.core.JmsTemplate; - import
org.springframework.jms.listener.DefaultMessageListenerCo ntainer; -
- public
class TestListener implements MessageListener{ -
-
private JmsTemplate jmsTemplate; -
-
private JmsTemplate jmsTemplate2; -
-
private String topicName; -
-
public TestListener(JmsTemplate jmsTemplate,String topicName,JmsTemplate jmsTemplate2){ -
-
this.jmsTemplate = jmsTemplate; -
-
this.topicName = topicName; -
-
this.jmsTemplate2 = jmsTemplate2; -
-
Topic topic; -
try { -
topic = this.jmsTemplate.getConnectionFactory().createConnection().createSession(false, -
Session.AUTO_ACKNOWLEDGE).createTopic(this.topicName); -
-
DefaultMessageListenerCo ntainer dmc = new DefaultMessageListenerCo ntainer(); -
dmc.setPubSubDomain(true); -
dmc.setDestination(topic); -
dmc.setConnectionFactory(this.jmsTemplate2.getConnectionFactory()); -
dmc.setPubSubNoLocal(true); -
dmc.setMessageListener(this); -
dmc.setSessionAcknowledgeMod e(Session.AUTO_ACKNOWLEDGE); -
dmc.initialize(); -
dmc.start(); -
} catch (JMSException e) { -
e.printStackTrace(); -
} -
} -
-
@Override -
public void onMessage(Message message) { -
-
System.out.println(message); -
} -
- }
- <bean
id="testListener" class="net.kentop.test.jms.TestListener"> - <constructor-arg
ref="jmsTemplate"></constructor-arg> - <constructor-arg
value="testTopic"></constructor-arg> - <constructor-arg
ref="jmsTemplate2"></constructor-arg> - </bean>