我正在爲我的團隊的某個應用程序研究排隊解決方案。理想情況下,我們希望可以配置爲輕量級進程中代理(用於線程之間的低吞吐量消息傳遞)以及作爲外部代理。有沒有可以做到這一點的MQ服務器?大多數似乎需要設置爲外部實體。 ZeroMQ似乎最接近進程內解決方案,但似乎更像是「類固醇上的UDP套接字」,我們需要可靠的交付。是否有任何可以在Java進程中運行的MQ服務器?
回答
就像我們說的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>
的JUnit
測試:
@ContextConfiguration(locations = {"classpath:/activeMQ-context.xml"})
public class SpringActiveMQTest extends AbstractJUnit4SpringContextTests {
@Autowired
private JmsTemplate template;
@Autowired
private ActiveMQDestination destination;
@Test
public void testJMSFactory() {
/* sending a message */
template.convertAndSend(destination, "Hi");
/* receiving a message */
Object msg = template.receive(destination);
if (msg instanceof TextMessage) {
try {
System.out.println(((TextMessage) msg).getText());
} catch (JMSException e) {
System.out.println("Error : " + e.getMessage());
}
}
}
}
的依賴要添加到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>
這很好,謝謝。 –
我們在ActiveMQ中看到的問題(以及爲什麼我現在正在快速搜索替代方法):(a)當您關閉它時,它不會停止它的所有線程。 (b)其許多API自動啓動施工服務,同時也有類似start()的方法,使得API不清楚並可能對(a)有所貢獻。 (c)多線程的許多問題,到目前爲止,它們已經通過添加更多同步塊來「修復」。 (d)可疑的安全性,特別是在發現模式下使用時。 (專業提示:在辦公室周圍設置一個流氓消息隊列服務器以享受娛樂時光!) – Trejkaz
WebSphere MQ客戶端有能力執行multicast pub/sub。這提供了繞過隊列管理器的客戶端到客戶端功能,但需要隊列管理器來建立連接。
- 1. 是否有任何可以在所有服務器上運行的Hangfire作業?
- 2. 應用程序的服務是否可以運行在不同的進程中?
- 3. 是否有任何MQ服務器支持失敗/重試?
- 4. Apache Commons是否可以在所有服務器上運行?
- 5. 在UI線程中運行的服務中是否可運行
- 6. 是否可以在一臺物理服務器上運行多個mongodb進程/服務器/端口?
- 7. 是否有可能在監視任務後運行任務?
- 8. 是否有可能從Rails的另一臺服務器運行rake任務
- 9. node.js是否可以在任何託管服務提供商中運行?
- 10. 是否可以使用Jenkins服務器逐個運行自定義任務?
- 11. 如何檢查服務器是否可以訪問並運行?
- 12. 是否可以在node.js服務器上運行Three.js WebGLRenderer?
- 13. 是否可以在Heroku上運行bitcoind服務器?
- 14. 實習生是否真的可以在任何Selenium服務上運行?
- 15. 是否有正在運行的進程的服務器優勢日誌文件
- 16. 是否可以運行在Windows服務中託管的Orleans
- 17. 在進程運行時是否可以停止libxml解析器?
- 18. 是否有任何並行執行發生在SQL服務器?
- 19. 可以在服務器上運行Selenium以自動執行任務,還是需要瀏覽器才能運行
- 20. 是否有可能以編程方式檢查硒遠程Web驅動服務器是否正在運行
- 21. 是否可以從網站上的遠程服務器運行應用程序?
- 22. 是否可以在Flex應用程序中運行Java程序?
- 23. 如何檢查特定進程是否在遠程PC /服務器上運行?
- 24. 在沒有任何服務器的情況下,是否可以通過WAN進行點對點連接?
- 25. 使用Java Spring,是否可以運行在客戶端執行的服務?
- 26. 是否有可能在watch任務中運行gulp-notify?
- 27. 在服務器上的後臺進程中運行進程
- 28. 查找UNIX上是否運行進程(服務器)
- 29. 確定瘦服務器是否作爲守護進程運行
- 30. 是否存在未在Java上運行的CMIS服務器?
我認爲http://stackoverflow.com/questions/2507536/lightweight-jms-broker的答案包含有趣的信息(例如[ffmq](http://ffmq.sourceforge.net/index .html)建議)。 ActiveMQ雖然是另一個較重的候選人,但它也是可嵌入的。 – fvu
就像@fvu所說的,ActiveMQ比ZeroMQ稍重,但它作爲一個嵌入式過程非常有效。如果你使用的是Spring,設置起來非常簡單。 –
ZeroMQ在TCP(而不是UDP)之上運行,可以提供可靠的傳輸。但是,你是指一個持久隊列嗎?即支持光盤? –