/
FailureListener.java
64 lines (47 loc) · 2.26 KB
/
FailureListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.assimbly.gateway.event;
import java.util.EventObject;
import org.apache.camel.management.event.ExchangeFailedEvent;
import org.apache.camel.management.event.ExchangeFailureHandledEvent;
import org.apache.camel.support.EventNotifierSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
// This class listens to failure events in camel exchanges (routes) and send them to the websocket topic: topic/alert
// Check the following page for all EventObject instances of Camel: http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/management/event/package-summary.html
@Component
public class FailureListener extends EventNotifierSupport {
private final Logger log = LoggerFactory.getLogger(FailureListener.class);
@Autowired
private SimpMessageSendingOperations messagingTemplate;
public void notify(EventObject eventObject) throws Exception {
if (eventObject instanceof ExchangeFailureHandledEvent) {
ExchangeFailureHandledEvent exchangeFailedEvent = (ExchangeFailureHandledEvent) eventObject;
String flowId = exchangeFailedEvent.getExchange().getFromRouteId();
if(this.messagingTemplate!=null) {
this.messagingTemplate.convertAndSend("/topic/" + flowId + "/alert", flowId);
}else {
log.warn("Can't send alert to websocket. messagingTemplate=null");
}
}if (eventObject instanceof ExchangeFailedEvent) {
ExchangeFailedEvent exchangeFailedEvent = (ExchangeFailedEvent) eventObject;
String flowId = exchangeFailedEvent.getExchange().getFromRouteId();
if(this.messagingTemplate!=null) {
this.messagingTemplate.convertAndSend("/topic/" + flowId + "/alert", flowId);
}else {
log.warn("Can't send alert to websocket. messagingTemplate=null");
}
}
}
public boolean isEnabled(EventObject event) {
return true;
}
protected void doStart() throws Exception {
// noop
}
protected void doStop() throws Exception {
// noop
}
}