jms - 如何处理 JmsException /如何在 ActiveMQ 中设置 redeliveryPolicy?
问题描述
让我们看看当 jms 向消费者发送消息时存在一种情况,我们必须将其保存到 db,但是当 db 关闭时,由于 db 服务器关闭,我们无法将其保存到 db。那么如何确认 jms 再次发送消息?
解决方案
This issue raised by many people.I resolved this using below code snippet.
@Configuration
public class AppConfiguration {
@Bean
public JmsListenerContainerFactory<?> jmsContainerFactory(DefaultJmsListenerContainerFactoryConfigurer configurer) {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setClientID(clientId);
connectionFactory.setBrokerURL(brokerUrl);
CachingConnectionFactory cf = new CachingConnectionFactory(connectionFactory);
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(cf);
factory.setSubscriptionDurable(true);
configurer.configure(factory, cf);
return factory;
}
}
@Component("ApprovalSubsCriber")
public class ApprovalSubscriber implements SessionAwareMessageListener<TextMessage> {
@Override
@JmsListener(destination = "topic/jmsReplyTest1", containerFactory = "jmsContainerFactory", subscription = "ApprovalSubsCriber")
public void onMessage(TextMessage message, Session session) throws JMSException {
// This is the received message
System.out.println("Receive: " + message.getText());
// Let's prepare a reply message - a "ACK" String
ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
textMessage.setText("ACK");
System.out.println(session.getAcknowledgeMode());
if ("notexception".equals(message.getText())) {
session.commit();
} else {
session.rollback();//If exception comes
}
}
}
推荐阅读
- python-3.x - 重命名 Pandas DataFrame 中的列不起作用
- sql - SQL查询顺序由2个字段
- c# - CollectionView 不显示 API 数据名称
- mysql - 如果在 SET foreign_key_checks = 0; 之后插入,那些没有对应外键的行会发生什么?
- scala - 重写 JSValue 中的嵌套搜索
- postgresql - 在 pl/SQL 循环中创建数百万个表时如何防止“内存不足”异常?
- css - React 中的 JavaScript 修改 DOM
- bash - 即使在 bash 脚本中使用双引号,SED 扩展也不起作用
- python-3.x - 使用 selenium 对 javascript 呈现的网站进行 Web 抓取
- python - pip 安装要求版本/依赖冲突