응용 프로그램 시작, 응용 프로그램 종료 및 모든 분 (틱)에 대해 rabbitMQ에 메시지를 보내는 모니터 서비스가 있습니다.Spring 응용 프로그램 종료시 RabbitMQ 메시지 보내기
시작 및 틱 이벤트가 정상적으로 작동합니다. 클래스가 원래 작성되었을 때 시스템 종료 이벤트도 작동했습니다. 내가 사용하고
스프링 부팅 스타터 AMQP 이벤트가 DisposableBean
인터페이스의 destroy
방법에 해고되었다
1.3.3.RELEASE.
나는 또한 ApplicationListener<ContextClosedEvent>
인터페이스와 Lifecycle
인터페이스를 구현하는 시도
모두 위의 방법은 반환 :
java.lang.IllegalStateException : ApplicationContext를 폐쇄하고 ConnectionFactory에 더 이상 연결을 만들 수 없습니다.
나는 Lifecycle
인터페이스를 제안하는 https://jira.spring.io/browse/AMQP-536 버그 수정이 있음을 발견했습니다.
RabbitMQ 연결이 종료되기 전에 종료 이벤트 메시지를 보내려면 어떻게해야합니까?
편집 : 더 많은 정보와 최신 코드
응용 프로그램은 서로 다른 서버에 여러 연결 팩토리를 가지고있다. 모니터 서비스는 monitorRabbitTemplate
을 통해 RabbitMQ 서버와 연결됩니다.
MonitorService
전에 문제가 monitorRabbitTemplate
연결 팩토리에있는 것 같습니다. 수명주기/종료/폐기 이벤트가 발생합니다.
최신 코드 (Lifecycle
대신 ApplicationListener<ContextClosedEvent>
의 및 DisposableBean
사용) :
@Component
public class MonitorServiceImpl implements MonitorService , Lifecycle {
private static final Logger LOGGER = LoggerFactory.getLogger(MonitorServiceImpl.class);
private final RabbitTemplate monitorRabbitTemplate;
private final String queueName;
private final Gson gson = new Gson();
@Autowired
public MonitorServiceImpl(@Qualifier("monitorRabbitTemplate") final RabbitTemplate monitorRabbitTemplate,
@Value("${monitor.rabbitmq.queue.name:monitor}") final String queueName) {
this.monitorRabbitTemplate = monitorRabbitTemplate;
this.queueName = queueName;
}
@Scheduled(fixedDelay = 60000)
public void tick() {
try {
send(new Monitor(Status.INFO, "I am here"));
} catch (final Exception e) {
LOGGER.error("FAILED TO SEND TICK EVENT", e);
}
}
@Override
public void send(final Monitor monitor) {
try {
final Message message = MessageBuilder.withBody(gson.toJson(monitor).getBytes())
.setContentType("application/json").setPriority(0).setDeliveryMode(MessageDeliveryMode.PERSISTENT)
.build();
monitorRabbitTemplate.send(queueName, message);
} catch (final Exception e) {
LOGGER.error("FAILED TO SEND MONITOR EVENT", e);
LOGGER.error("FAILED TO SEND MONITOR EVENT to {}:{}", monitorRabbitTemplate.getConnectionFactory()
.getHost(), monitorRabbitTemplate.getConnectionFactory().getPort());
}
}
@Override
public void start() {
try {
send(new Monitor(Status.STARTING, "Application starting up..."));
} catch (final Exception e) {
LOGGER.error("FAILED TO SEND STARTUP EVENT", e);
}
}
@Override
public void stop() {
try {
send(new Monitor(Status.TERMINATING, "Application shutdown..."));
} catch (final Exception e) {
LOGGER.error("FAILED TO SEND SHUTDOWN EVENT", e);
}
}
@Override
public boolean isRunning() {
return true;
}
}