以编程方式启动 ActiveMQ,而不是作为服务启动



我正在使用ActiveMQ,我为一些消息创建了生产者和消费者。

这样我就可以创建连接,并创建目标:

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username,password,"tcp://localhost:61616");
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MyQueue");

通过这种方式,我创建生产者并发送消息:

Producer producer = session.createProducer(destination);
producer.send(msgToSend);

我创建使用者并为其设置一个侦听器(实现 MessageListener 接口的类)

Consumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);

Consummer连接到目的地,它正在收听消息。 当它从队列"MyQueue"中获取一些消息时,就会触发来自 MessageListener 的 onMessage() 方法,并对消息执行任何我想做的事情。

我的代码工作,我能够生成和使用消息。生产者在服务器上,使用者在客户端(一个单独的项目)上。

为了使其工作,我从这里安装了apache-activemq-5.14.4-bin.zip。 我把依赖放在pom.xml:

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.0</version>
</dependency> 

好吧,现在安装的ActiveMQ是在计算机启动时启动的服务。我不想将其作为服务安装,而是在 java 中以编程方式启动并停止它。对于检查,请按"开始"按钮并执行代码以启动它并预先停止并停止它。

我可以通过编程方式实现而不将 ActiveMQ 安装为服务吗?

请点击链接以编程方式启动和停止代理 http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html

是的,您可以通过编程方式使用 ActiveMQ(例如在测试中)。以下是一些详细信息: http://activemq.apache.org/how-to-unit-test-jms-code.html

相关内容

最新更新