1.使用的是maven的结构,导入activeMQ的包
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.5.0</version> </dependency>
2.创建“生产者”
①xml配置文件
<!-- 创建工厂连接 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- activeMQ消息目标 队列 --> <bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg> </bean> <!-- activeMQ消息目标 主题--> <!-- <bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQTopic">--> <!-- <constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg>--> <!-- </bean>--> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="marketingGateway" class="com.jms.service.RantzMarketingGatewayImpl"> <property name="jmsTemplate" ref="jmsTemplate" /> <property name="destination" ref="rantzDestination" /> </bean>
②java代码
主类SpringMain
public static void main(String[] args) { /*开始加载spring配置文件*/ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); IRantzMarketingGateway rantzMarketingGateway= (RantzMarketingGatewayImpl) context.getBean("marketingGateway"); rantzMarketingGateway.sendMotoristInfo(); System.out.println("Start ..."); }
消息发送类RantzMarketingGatewayImpl
import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class RantzMarketingGatewayImpl implements IRantzMarketingGateway { private JmsTemplate jmsTemplate; private Destination destination; public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public Destination getDestination() { return destination; } public void setDestination(Destination destination) { this.destination = destination; } public void sendMotoristInfo(){ jmsTemplate.send( destination, new MessageCreator(){ public Message createMessage(Session session) throws JMSException { return session.createTextMessage("这是一个测试"); } } ); } }
3.创建“消费者”
①xml配置文件
<!-- 创建工厂连接 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- activeMQ消息目标 队列 --> <bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg> </bean> <!-- activeMQ消息目标 主题--> <!-- <bean id="rantzDestination" class="org.apache.activemq.command.ActiveMQTopic">--> <!-- <constructor-arg index="0" value="rantz.marketing.queue"></constructor-arg>--> <!-- </bean>--> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="rantzDestination" /> </bean> <bean id="marketingGateway" class="com.huateng.jms.service.MarketingReceiverGatewayImpl"> <property name="jmsTemplate" ref="jmsTemplate" /> </bean>
②java代码
主类SpringMain
public static void main(String[] args) { /*开始加载spring配置文件*/ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); MarketingReceiverGatewayImpl rantzMarketingGateway= (MarketingReceiverGatewayImpl) context.getBean("marketingGateway"); System.out.println("Receive Start ..."); try { rantzMarketingGateway.receiveMotorist(); } catch (Exception e) { e.printStackTrace(); } }
消息发送类MarketingReceiverGatewayImpl
import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; public class MarketingReceiverGatewayImpl { private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public MarketingReceiverGatewayImpl() { } public void receiveMotorist() throws Exception{ TextMessage message = (TextMessage)jmsTemplate.receive(); System.out.println("reviced msg is:" + message.getText()); } }
4.启动activeQM
在下载的activeQM包中的bin目录中,可以找到一个用于启动activeMQ的脚本。unix用户使用activemq;windows用户使用activemq.bat
5.先运行"生产者",再运行"消费者"
6.结果
Start ...
Receive Start ...
reviced msg is:这是一个测试