-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
KafkaEventEmitter.java
139 lines (126 loc) · 6.04 KB
/
KafkaEventEmitter.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.event.emitters.kafka;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.cloudevents.core.builder.CloudEventBuilder;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.jbpm.persistence.api.integration.EventCollection;
import org.jbpm.persistence.api.integration.EventEmitter;
import org.jbpm.persistence.api.integration.InstanceView;
import org.jbpm.persistence.api.integration.base.BaseEventCollection;
import org.jbpm.persistence.api.integration.model.CaseInstanceView;
import org.jbpm.persistence.api.integration.model.ProcessInstanceView;
import org.jbpm.persistence.api.integration.model.TaskInstanceView;
/**
* Kafka implementation of EventEmitter that simply pushes out data to Kafka topic.
*
* This event emitter expects following parameters to configure itself - via system properties
* <ul>
* <li>org.jbpm.event.emitters.kafka.date_format - date and time format to be sent to ElasticSearch - default format is yyyy-MM-dd'T'HH:mm:ss.SSSZ</li>
* <li>org.jbpm.event.emitters.kafka.boopstrap.servers - kafka server ip, default is localhost:9092</li>
* <li>org.jbpm.event.emitters.kafka.topic.<processes|tasks|cases>. Topic name for subscribing to these events. Defaults are "jbpm-<processes|tasks|cases>-events"</li>
* </ul>
*/
public class KafkaEventEmitter implements EventEmitter {
private static final String SOURCE_FORMATTER = "/process/%s/%s";
private Producer<String, byte[]> producer;
private ObjectMapper mapper;
public KafkaEventEmitter() {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, System.getProperty(
"org.jbpm.event.emitters.kafka.boopstrap.servers", "localhost:9092"));
configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName());
producer = new KafkaProducer<>(configs);
mapper = new ObjectMapper()
.setDateFormat(new SimpleDateFormat(System.getProperty(
"org.jbpm.event.emitters.kafka.date_format", System.getProperty(
"org.kie.server.json.date_format",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"))))
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
}
public void deliver(Collection<InstanceView<?>> data) {
// no-op
}
public void apply(Collection<InstanceView<?>> data) {
if (data == null || data.isEmpty()) {
return;
}
for (InstanceView<?> view : data) {
String processId;
long processInstanceId;
String type;
String topic;
if (view instanceof ProcessInstanceView) {
ProcessInstanceView processInstanceView = (ProcessInstanceView) view;
topic = "processes";
type = "process";
processInstanceId = processInstanceView.getId();
processId = processInstanceView.getProcessId();
} else if (view instanceof TaskInstanceView) {
TaskInstanceView taskInstanceView = (TaskInstanceView) view;
type = "tasks";
topic = "task";
processInstanceId = taskInstanceView.getProcessInstanceId();
processId = taskInstanceView.getProcessId();
} else if (view instanceof CaseInstanceView) {
CaseInstanceView caseInstanceView = (CaseInstanceView) view;
type = "case";
topic = "cases";
processInstanceId = caseInstanceView.getId();
processId = caseInstanceView.getCaseId();
} else {
throw new UnsupportedOperationException("view " + view.getClass());
}
try {
producer.send(new ProducerRecord<>(getTopic(topic), mapper.writeValueAsBytes(CloudEventBuilder.v03()
.withSource(URI.create(String.format(SOURCE_FORMATTER, processId, processInstanceId))).withType(
type).withData(mapper.writeValueAsBytes(view)).withId(UUID.randomUUID().toString())
.withTime(OffsetDateTime.now()).build())));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("cannot convert " + view + " to byte[]", e);
}
}
}
private static String getTopic(String eventType) {
return System.getProperty("org.jbpm.event.emitters.kafka.topic." + eventType, "jbpm-" + eventType + "-events");
}
public void drop(Collection<InstanceView<?>> data) {
// no-op
}
@Override
public void close() {
producer.close();
}
@Override
public EventCollection newCollection() {
return new BaseEventCollection();
}
}