Produce/consume messages in KAFKA with SAP Netweav...
Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Now this StepByStepServer.java file is a combination of the SAP example "StepByStepServer.java" and some code I copied from here. I didn't spend too much time on making the code pretty and neat. I just did the necessary to make it work so please don't judge.
// Assign topicName to string variable
String topicName = "my-kafka-topic";
// create instance for properties to access producer configs
Properties props = new Properties();
// Assign localhost id
props.put("bootstrap.servers", "localhost:9092");
// Set acknowledgements for producer requests.
props.put("acks", "all");
// If the request fails, the producer can automatically retry,
props.put("retries", 0);
// Specify buffer size in config
props.put("batch.size", 16384);
// Reduce the no of requests less than 0
props.put("linger.ms", 1);
// The buffer.memory controls the total amount of memory available to the
// producer for buffering.
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(props);
producer.send(new ProducerRecord<String, String>(topicName, message, message));
function.getExportParameterList().setValue("RESPTEXT", "Message sent successfully");
System.out.println("Message sent successfully");
// producer.close();
What the code does is take the text passed from the STFC_CONNECTION function module, and call the Apache KAFKA producer API with it. It's that simple.
Compile and run. Notice I need to put both JCo and KAFKA libraries in the classpath now.
Consume a message using the Java connector client and call RFC
Now let's try the other way. Someone changed a transaction outside and published to a KAFKA topic and SAP wants to know about it and do something with it.
The flow is:
Message is produced to KAFKA topic
Java client (with Java Connector) consumes the message.
Java client calls SAP RFC
SAP RFC do something with the message.
I copied the Java code from "Tutorialspoint.com" and put it inside StepByStepServer.java provided by SAP. See SimpleProducer.java from https://www.tutorialspoint.com/apache_kafka/
SapKafkaConsumer.java is a copy of the SimpleConsumer.java which I borrowed from here as mentioned, combined with the code from the StepByStepClient.java from the SAP example.
The code can already consume a message from a KAFKA topic "my-kafka-topic", and I take that message and call function STFC_CONNECTION in SAP with the message.
The function will echo back the text showing it has successfully received it.
In the doWork method which is called when a message is received, I've added the code to call function STFC_CONNECTION. It should be straight forward what the code does.
In summary, it is possible and also not too difficult to do so.
What we now need to explore is how to productionize this solution, how to make this HA/DR etc. I'm still have some unanswered questions on how this can handle massive volume in an enterprise environment, or whether the JCo server and client should be on a separate instance.
Anyway, thanks for you time, I hope you find this interesting. Leave me some comments below.