我正在使用ActiveMQ Artemis和JMS 2.0与共享的使用者阅读主题消息。我有两个问题:
while
循环?如果我不使用while (true)
循环,则程序将在主题无消息时终止。SharedConsumer.java
public class SharedConsumer {
@Resource(lookup = "java:comp/DefaultJMSConnectionFactory")
ConnectionFactory connectionFactory;
public String maxConnectionForJSON;
public void readFromTopicAndSendToQueue()throws Exception{
Context initialContext = null;
JMSContext jmsContext = null;
int maxConnectionCount = 0;
maxConnectionForJSON = "30";
if (!StringUtils.isBlank(maxConnectionForJSON)){
try{
maxConnectionCount = Integer.parseInt(maxConnectionForJSON);
}catch (Exception e){
//logging
}
}
if (maxConnectionCount != 0) {
try {
List<JMSConsumer> jmsConsumerList = new ArrayList<>();
initialContext = new InitialContext();
Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
jmsContext = cf.createContext("admin", "admin");
for (int i = 0; i < maxConnectionCount; i++){
JMSConsumer jmsConsumer = jmsContext.createSharedDurableConsumer(topic, "ct");
MessageListener listener = new Listener();
jmsConsumer.setMessageListener(listener);
}
while (true) {
Thread.sleep(30000);
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
if (initialContext != null) {
initialContext.close();
}
if (jmsContext != null) {
jmsContext.close();
}
}
}
}
public static void main(final String[] args) throws Exception {
SharedConsumer sharedConsumer = new SharedConsumer();
sharedConsumer.readFromTopicAndSendToQueue();
}
}
SharedConsumerListener.java
public class Listener implements MessageListener {
public static int count = 0;
@Override
public void onMessage(Message message) {
System.out.println(message.toString() + "\ncount :" + count);
count++;
}
}
我可以使用xml文件读取JMS 1.1(ActiveMQ)中的队列。我以为我们可以在JMS 2.0 Artemis中使用如下配置文件,但我错了。非常感谢您对Justin Bertram的帮助。
在JMS 1.1配置文件中
<bean id="brokerUrl" class="java.lang.String">
<constructor-arg value="#{appProperties.queueUrl}"/>
</bean>
<amq:connectionFactory id="amqConnectionFactory" brokerURL="#brokerUrl" dispatchAsync="true"/>
<bean id="connectionFactory1" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<constructor-arg ref="amqConnectionFactory"/>
<property name="maxConnections" value="#{appProperties.maxConnections}"/>
<property name="idleTimeout" value="#{appProperties.idleTimeout}"/>
<property name="maximumActiveSessionPerConnection" value = "10"/>
</bean>
<bean id="jmsForQueue" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory1"/>
</bean>
<bean id="jSONQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="#{appProperties.queueName}"/>
</bean>
<task:executor id="mainExecutorForJSON" pool-size="#{appProperties.mainExecutorForJSONPoolSize}"
queue-capacity="0" rejection-policy="CALLER_RUNS"/>
<int:channel id="jmsInChannelForJSON" >
<int:dispatcher task-executor="mainExecutorForJSON"/>
</int:channel>
<int-jms:message-driven-channel-adapter id="jmsInForJSON" destination="jSONNrtQueue" channel="jmsInChannelForJSON"
concurrent-consumers="#{appProperties.concurrentConsumerCountForJSON}" />
<int:service-activator input-channel="jmsInChannelForJSON" ref="dataServiceJMS" />
答案 0 :(得分:0)
简而言之,是的,一旦设置了JMS使用者的消息侦听器,阻止程序终止是正常的。
创建JMS使用者并设置其消息侦听器时,JMS客户端实现将在后台创建新线程,以异步地从创建使用者并设置侦听器的线程中侦听消息。因此,创建使用者并设置侦听器的线程将继续进行。在您的情况下,您需要以某种方式阻止线程退出并终止应用程序,因此您需要使用while
循环。