44
44
import com .fasterxml .jackson .databind .SerializationFeature ;
45
45
46
46
/**
47
- * Kafka implementation of EventEmitter that simply pushes out data to several Kafka topics depending on InstanceView type.
47
+ * Kafka implementation of EventEmitter that simply pushes out data to several Kafka topics depending on InstanceView type.
48
48
*
49
49
* Expects following parameters to configure itself - via system properties
50
50
* <ul>
51
- * <li>org.kie.jbpm.event.emitters.kafka.date_format - date and time format to be sent to Kafka - default format is yyyy-MM-dd'T'HH:mm:ss.SSSZ</li>
52
- * <li>org.kie.jbpm.event.emitters.kafka.bootstrap.servers - Kafka server ip, default is localhost:9092</li>
53
- * <li>org.kie.jbpm.event.emitters.kafka.client.id - Kafka client id</li>
54
- * <li>org.kie.jbpm.event.emitters.kafka.acks - Kafka acknowledge policy, check <a href="http://kafka.apache.org/documentation.html#producerconfigs">Kafka documentation</a></li>
55
- * <li>org.kie.jbpm.event.emitters.kafka.topic.<processes|tasks|cases>. Topic name for subscribing to these events. Defaults are "jbpm-<processes|tasks|cases>-events"</li>
56
- * </ul>
51
+ * <li>org.kie.jbpm.event.emitters.kafka.date_format - date and time format to be sent to Kafka - default format is yyyy-MM-dd'T'HH:mm:ss.SSSZ</li>
52
+ * <li>org.kie.jbpm.event.emitters.kafka.bootstrap.servers - Kafka server ip, default is localhost:9092</li>
53
+ * <li>org.kie.jbpm.event.emitters.kafka.client.id - Kafka client id</li>
54
+ * <li>org.kie.jbpm.event.emitters.kafka.acks - Kafka acknowledge policy, check <a href="http://kafka.apache.org/documentation.html#producerconfigs">Kafka documentation</a></li>
55
+ * <li>org.kie.jbpm.event.emitters.kafka.topic.<processes|tasks|cases>. Topic name for subscribing to these events. Defaults are "jbpm-<processes|tasks|cases>-events"</li>
56
+ * </ul>
57
57
*/
58
58
public class KafkaEventEmitter implements EventEmitter {
59
59
@@ -62,7 +62,7 @@ public class KafkaEventEmitter implements EventEmitter {
62
62
protected static final String KAFKA_EMITTER_PREFIX = "org.kie.jbpm.event.emitters.kafka." ;
63
63
64
64
private ObjectMapper mapper ;
65
-
65
+
66
66
private KafkaSender sender ;
67
67
68
68
private Producer <String , byte []> producer ;
@@ -73,10 +73,10 @@ public KafkaEventEmitter() {
73
73
74
74
KafkaEventEmitter (Producer <String , byte []> producer ) {
75
75
this .producer = producer ;
76
- this .sender = Boolean .getBoolean (KAFKA_EMITTER_PREFIX + "sync" ) ? this ::sendSync : this ::sendAsync ;
77
- mapper = new ObjectMapper ()
76
+ this .sender = Boolean .getBoolean (KAFKA_EMITTER_PREFIX + "sync" ) ? this ::sendSync : this ::sendAsync ;
77
+ this . mapper = new ObjectMapper ()
78
78
.setDateFormat (new SimpleDateFormat (System .getProperty (
79
- KAFKA_EMITTER_PREFIX + "date_format" , System .getProperty (
79
+ KAFKA_EMITTER_PREFIX + "date_format" , System .getProperty (
80
80
"org.kie.server.json.date_format" ,
81
81
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" ))))
82
82
.configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false )
@@ -95,38 +95,45 @@ public void apply(Collection<InstanceView<?>> data) {
95
95
}
96
96
97
97
for (InstanceView <?> view : data ) {
98
- String processId ;
99
- long processInstanceId ;
100
- String type ;
101
- String topic ;
102
- if (view instanceof ProcessInstanceView ) {
103
- ProcessInstanceView processInstanceView = (ProcessInstanceView ) view ;
104
- topic = "processes" ;
105
- type = "process" ;
106
- processInstanceId = processInstanceView .getId ();
107
- processId = processInstanceView .getProcessId ();
108
- } else if (view instanceof TaskInstanceView ) {
109
- TaskInstanceView taskInstanceView = (TaskInstanceView ) view ;
110
- topic = "tasks" ;
111
- type = "task" ;
112
- processInstanceId = taskInstanceView .getProcessInstanceId ();
113
- processId = taskInstanceView .getProcessId ();
114
- } else if (view instanceof CaseInstanceView ) {
115
- CaseInstanceView caseInstanceView = (CaseInstanceView ) view ;
116
- topic = "cases" ;
117
- type = "case" ;
118
- processInstanceId = caseInstanceView .getId ();
119
- processId = caseInstanceView .getCaseDefinitionId ();
120
- } else {
121
- logger .warn ("Unsupported view type {}" , view .getClass ());
122
- continue ;
98
+ try {
99
+ String processId ;
100
+ long processInstanceId ;
101
+ String type ;
102
+ String topic ;
103
+ if (view instanceof ProcessInstanceView ) {
104
+ ProcessInstanceView processInstanceView = (ProcessInstanceView ) view ;
105
+ topic = "processes" ;
106
+ type = "process" ;
107
+ processInstanceId = processInstanceView .getId ();
108
+ processId = processInstanceView .getProcessId ();
109
+ } else if (view instanceof TaskInstanceView ) {
110
+ TaskInstanceView taskInstanceView = (TaskInstanceView ) view ;
111
+ topic = "tasks" ;
112
+ type = "task" ;
113
+ processInstanceId = taskInstanceView .getProcessInstanceId ();
114
+ processId = taskInstanceView .getProcessId ();
115
+ } else if (view instanceof CaseInstanceView ) {
116
+ CaseInstanceView caseInstanceView = (CaseInstanceView ) view ;
117
+ topic = "cases" ;
118
+ type = "case" ;
119
+ processInstanceId = caseInstanceView .getId ();
120
+ processId = caseInstanceView .getCaseDefinitionId ();
121
+ } else {
122
+ logger .warn ("Unsupported view type {}" , view .getClass ());
123
+ continue ;
124
+ }
125
+ logger .debug ("Sending view to topic {} type {} processId {} pid {} and view {}" , topic , type , processId , processInstanceId , view );
126
+ sender .send (topic , type , processId , processInstanceId , view );
127
+ logger .debug ("Sucessfuly view sent view to topic {} type {} processId {} pid {} and view {}" , topic , type , processId , processInstanceId , view );
128
+ } catch (Throwable th ) {
129
+ logError (view , th );
123
130
}
124
- sender . send ( topic , type , processId , processInstanceId , view );
131
+
125
132
}
126
133
}
127
-
134
+
128
135
private interface KafkaSender {
129
- void send (String topic , String type , String processId , long processInstanceId , InstanceView <?> view );
136
+ void send (String topic , String type , String processId , long processInstanceId , InstanceView <?> view );
130
137
}
131
138
132
139
private byte [] viewToPayload (String type , String processId , long processInstanceId , InstanceView <?> view ) throws JsonProcessingException {
@@ -140,7 +147,7 @@ private void sendAsync(String topic, String type, String processId, long process
140
147
logError (view , e );
141
148
}
142
149
});
143
- } catch (Exception e ) {
150
+ } catch (Throwable e ) {
144
151
logError (view , e );
145
152
}
146
153
}
@@ -162,11 +169,10 @@ private void sendSync(String topic, String type, String processId, long processI
162
169
}
163
170
}
164
171
165
- private void logError (InstanceView <?> view , Exception e ) {
172
+ private void logError (InstanceView <?> view , Throwable e ) {
166
173
logger .error ("Error publishing view {}" , view , e );
167
174
}
168
175
169
-
170
176
@ Override
171
177
public void drop (Collection <InstanceView <?>> data ) {
172
178
// nothing to do
@@ -191,7 +197,7 @@ private static Producer<String, byte[]> getProducer() {
191
197
192
198
private static String getTopic (String eventType ) {
193
199
return System .getProperty ("org.kie.jbpm.event.emitters.kafka.topic." + eventType , "jbpm-" + eventType +
194
- "-events" );
200
+ "-events" );
195
201
}
196
202
197
203
protected static Map <String , Object > getProducerProperties () {
0 commit comments