我为我的项目开发了两个Web应用程序作为生产者和消费者。 除了一些消息处于挂起状态(我可以在浏览器/ UI中看到),即使在我的监听器消耗了所有消息之后,一切正常。
Exp: - 制作人发送10K,消费者收到10K,但我仍然可以看到约有1K的消息处于待处理状态。如果我在UI中单击清除选项,那么我的监听器再次监听那些待处理的消息。
减少预取政策会减少待处理的消息,但会影响我的消费者的表现。
制片人代码:
@Bean
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(env.getProperty(JioTUConstant.SYS_JMS_BROKER_URL));
connectionFactory.setUseAsyncSend(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_USE_ASYNC_SEND)));
connectionFactory.setAlwaysSessionAsync(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_ALWAYS_SESSION_ASYNC)));
connectionFactory.setOptimizeAcknowledge(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_OPTIMIZE_ACK)));
connectionFactory.setUserName(env.getProperty(JioTUConstant.SYS_JMS_USERNAME));
connectionFactory.setPassword(env.getProperty(JioTUConstant.SYS_JMS_PASSWORD));
return connectionFactory;
}
@Bean
public CachingConnectionFactory cachingConnectionFactory(){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(connectionFactory());
cachingConnectionFactory.setSessionCacheSize(new Integer(env.getProperty(JioTUConstant.SYS_JMS_SESSION_CACHE_SIZE)));
cachingConnectionFactory.setReconnectOnException(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_RECONNECT_ON_EXCEPTION)));
return cachingConnectionFactory;
}
@Bean
public JmsTemplate jmsTemplate(){
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDeliveryMode(new Integer(env.getProperty(JioTUConstant.SYS_JMS_DELIVERY_MODE)));//DeliveryMode.NON_PERSISTENT=1 and DeliveryMode.PERSISTENT=2
jmsTemplate.setSessionAcknowledgeMode(new Integer(env.getProperty(JioTUConstant.SYS_JMS_SESSION_ACK_MODE)));//Session.AUTO_ACKNOWLEDGE=1,Session.CLIENT_ACKNOWLEDGE=2,Session.DUPS_OK_ACKNOWLEDGE=3 and Session.SESSION_TRANSACTED=0
jmsTemplate.setSessionAcknowledgeModeName(env.getProperty(JioTUConstant.SYS_JMS_SESSION_ACK_MODE_NAME));
jmsTemplate.setSessionTransacted(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_SESSION_TRANSACTED)));
jmsTemplate.setDefaultDestinationName(env.getProperty(JioTUConstant.SYS_JMS_LOG_QUEUE_NAME));
jmsTemplate.setConnectionFactory(cachingConnectionFactory());
return jmsTemplate;
}
消费者代码:
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConcurrency(env.getProperty(JioTUConstant.SYS_JMS_CONCURRENT_LISTENER));
factory.setConnectionFactory(cachingConnectionFactory());
return factory;
}
@Bean
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(env.getProperty(JioTUConstant.SYS_JMS_BROKER_URL));
connectionFactory.setAlwaysSessionAsync(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_ALWAYS_SESSION_ASYNC)));
connectionFactory.setOptimizeAcknowledge(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_OPTIMIZE_ACK)));
connectionFactory.setUserName(env.getProperty(JioTUConstant.SYS_JMS_USERNAME));
connectionFactory.setPassword(env.getProperty(JioTUConstant.SYS_JMS_PASSWORD));
return connectionFactory;
}
@Bean
public CachingConnectionFactory cachingConnectionFactory(){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setSessionCacheSize(new Integer(env.getProperty(JioTUConstant.SYS_JMS_SESSION_CACHE_SIZE)));
cachingConnectionFactory.setReconnectOnException(new Boolean(env.getProperty(JioTUConstant.SYS_JMS_RECONNECT_ON_EXCEPTION)));
cachingConnectionFactory.setTargetConnectionFactory(connectionFactory());
return cachingConnectionFactory;
}
ActiveMQ 5.14.0