一旦我使用了来自rabbitmq的消息,我已经实现了多线程来在jira中执行一些操作。我正在使用spring amqp(版本1.6.1)
一旦线程捕获异常,我将状态设置为错误,并在将来引用输出对象。将此输出对象发送到队列时。我面临着上面的执行
代码:
连接工厂:
@Configuration
@PropertySources({ @PropertySource("classpath:application.properties") })
public class RabbitMQConfiguration {
@Autowired
private Environment environment;
@Bean
public ConnectionFactory connectionFactory() {
// TODO make it possible to customize in subclasses.
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(environment.getProperty("bip.rabbitmq.url"));
connectionFactory.setUsername(environment.getProperty("bip.rabbitmq.username"));
connectionFactory.setPassword(environment.getProperty("bip.rabbitmq.password"));
return connectionFactory;
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
/**
* @return the admin bean that can declare queues etc.
*/
@Bean
public AmqpAdmin amqpAdmin() {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
return rabbitAdmin;
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(jsonMessageConverter());
return template;
}
@Bean(name = "jiraQueueListenerContainerFactory")
public SimpleRabbitListenerContainerFactory jiraQueueListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMessageConverter(new Jackson2JsonMessageConverter());
factory.setReceiveTimeout(10L);
return factory;
}
}
MessageHandler:
@Component
@PropertySources({ @PropertySource("classpath:application.properties") })
public class JiraMessageHandler {
@Autowired
private RabbitTemplate rabbitTemplate;
private static ExecutorService executor = Executors.newFixedThreadPool(Constants.THREAD_SIZE);
private static Logger logger = LogManager.getLogger(JiraMessageHandler.class);
@RabbitListener(containerFactory = "jiraQueueListenerContainerFactory", queues = Constants.QUEUE_NAME)
public void handleMessage(HashMap<String, Object> jiraMessage) {
logger.info(jiraMessage.toString() + System.currentTimeMillis());
logger.info("Jira Message Handler");
BaseServerAdapter jiraProcessingAdapter = new JiraProcessingAdapter();
Future future = executor.submit(jiraProcessingAdapter);
JiraAdapterOutput jiraAdapterOutput = new JiraAdapterOutput();
Future future = executor.submit(jiraProcessingAdapter);
jiraAdapterOutput = (JiraAdapterOutput) future.get();
try {
jiraAdapterOutput = (JiraAdapterOutput) future.get();
if (jiraAdapterOutput.getOutputMap().get("activityStatus") == "SUCCESS") {
logger.info("Successfully Executed Jira ::: " + new Date() + "::: "
+ jiraAdapterOutput.getOutputMap().get("jiraId"));
rabbitTemplate.convertAndSend(Constants.ADAPTER_OUTPUT_QUEUE, senderMap);
}else if (jiraAdapterOutput.getOutputMap().get("activityStatus").equalsIgnoreCase("FAIL")) {
logger.info("Successfully Executed Jira ::: " + new Date() + "::: "
+ jiraAdapterOutput.getOutputMap().get("jiraId"));
sendMessageForProcessingToBIP(senderMap);
}
private boolean sendMessageForProcessingToBIP(HashMap<String, ExchangeDTO> senderMap) {
try {
rabbitTemplate.convertAndSend(Constants.WFM_ERROR_QUEUE, senderMap);
return true;
} catch (Exception e) {
**logger.info("Message sending failed, try again:::::::" + e.getMessage());**
}
return false;
}
显示“应用程序上下文已关闭且ConnectionFactory无法再创建连接。”
我做错了什么。 我在这里也提到过:https://jira.spring.io/browse/AMQP-546
答案 0 :(得分:0)
上面的代码(在handleMessage中)看起来不完整 - 尝试没有任何问题。
在尝试发送之前,您必须在其他位置关闭应用程序上下文。关闭/销毁后,您不能在上下文中使用bean。您引用的JIRA允许在其他bean的stop()
方法中进行此类访问 - 即仅在所有其他bean停止后才销毁连接。
我建议您打开org.springframework
的DEBUG日志记录,找出上下文过早关闭的原因。