是否有任何MQ服务器可以嵌入Java进程中运行?
我正在为我团队的一个应用程序研究排队解决方案。理想情况下,我们希望能够将其配置为轻量级进程内代理(用于线程之间的低吞吐量消息传递)和外部代理。有没有一个MQ服务器可以做到这一点?大多数似乎需要设置为外部实体。ZeroMQ似乎最接近于进程内解决方案,但它似乎更像是“类固醇上的UDP套接字”,我们需要可靠的交付 WebSphere MQ客户端能够执行以下操作:。这提供了绕过队列管理器的客户端到客户端功能,尽管建立连接需要队列管理器。正如我们所说的是否有任何MQ服务器可以嵌入Java进程中运行?,java,jms,message-queue,amqp,mq,Java,Jms,Message Queue,Amqp,Mq,我正在为我团队的一个应用程序研究排队解决方案。理想情况下,我们希望能够将其配置为轻量级进程内代理(用于线程之间的低吞吐量消息传递)和外部代理。有没有一个MQ服务器可以做到这一点?大多数似乎需要设置为外部实体。ZeroMQ似乎最接近于进程内解决方案,但它似乎更像是“类固醇上的UDP套接字”,我们需要可靠的交付 WebSphere MQ客户端能够执行以下操作:。这提供了绕过队列管理器的客户端到客户端功能,尽管建立连接需要队列管理器。正如我们所说的ActiveMQ比ZeroMQ重一点,但它作为嵌入式进
ActiveMQ
比ZeroMQ
重一点,但它作为嵌入式进程工作得非常好。
下面是一个使用Spring
和ActiveMQ
的简单示例
将用于测试队列的消息侦听器:
public class TestMessageListener implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(TestMessageListener.class);
@Override
public void onMessage(Message message) {
/* Receive the text message */
if (message instanceof TextMessage) {
try {
String text = ((TextMessage) message).getText();
System.out.println("Message reception from the JMS queue : " + text);
} catch (JMSException e) {
logger.error("Error : " + e.getMessage());
}
} else {
/* Handle non text message */
}
}
}
ActiveMQ
上下文配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jmsQueueConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61617</value>
</property>
</bean>
<bean id="pooledJmsQueueConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<constructor-arg ref="jmsQueueConnectionFactory" />
</bean>
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="messageQueue" />
</bean>
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="pooledJmsQueueConnectionFactory" />
<property name="pubSubDomain" value="false"/>
</bean>
<bean id="testMessageListener" class="com.example.jms.TestMessageListener" />
<bean id="messageQueuelistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="pooledJmsQueueConnectionFactory" />
<property name="destination" ref="QueueDestination" />
<property name="messageListener" ref="testMessageListener" />
<property name="concurrentConsumers" value="5" />
<property name="acceptMessagesWhileStopping" value="false" />
<property name="recoveryInterval" value="10000" />
<property name="cacheLevelName" value="CACHE_CONSUMER" />
</bean>
</beans>
要添加到pom.xml
的依赖项:
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- ActiveMQ -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.6.0</version>
</dependency>
org.springframework
SpringJMS
${org.springframework版本}
org.apache.activemq
activemq全部
5.6.0
编译
org.apache.activemq
activemq池
5.6.0
org.apache.activemq
activemq内核
5.6.0
我认为答案应该包含有趣的信息(如建议)。ActiveMQ是另一个更重的候选者,但它也是可嵌入的。正如@fvu所说,ActiveMQ比ZeroMQ重一点,但它作为一个嵌入式进程工作得非常好。如果您使用的是Spring,那么设置起来就很容易了。ZeroMQ运行在TCP(而不是UDP)之上,TCP提供了可靠的传输。但是,您指的是持久队列吗?例如,备份到光盘?我们不关心邮件本身的持久性,但我们希望传递尽可能可靠。性能还不是问题。我假设这意味着队列应该是持久的。@fvu感谢您的输入,我将使用ActiveMQ。您能否重新发布作为答案,以便我可以接受?我们在ActiveMQ中看到的问题(以及为什么我目前正在快速搜索替代品):(a)当您关闭它时,它不会停止所有线程。(b) 它的许多API在构造时会自动启动服务,同时也有类似start()的方法,这使得API不清楚,并且可能导致(a)。(c) 多线程的许多问题,到目前为止已经通过添加更多的同步块“修复”了。(d) 可疑的安全性,尤其是在发现模式下使用时。(专业提示:设置一个流氓消息队列服务器,让您在办公室里玩得开心!)您知道线程关闭问题是否已得到解决?你还在使用这个解决方案吗?
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- ActiveMQ -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.6.0</version>
</dependency>