Using Kafka Producer inside oracle DB

1.4k Views Asked by At

I am trying to post Messages from Oracle to Kafka broker directly using custom java class for that I uploaded the required library in oracle and calling java function using oracle function/procedure. Basic java function is working fine but function in which Kafka is getting initialized and used throwing uncaught exception. Even I put whole function body inside try-catch block but still not getting my customized exception as message.

I am having Oracle 12C that have JVM version 1.8.

This message got popup on oracle SQL prompt at java function call (java.lang.NoClassDefFound Error).

I created a jar file for my custom classes and uploaded it with it's respected libraries to oracle DB. I uploaded library files using below command.

loadjava -u <user>/<Password>@DB -resolve <library>.jar 

Please tell how to get Kafka Producer initialized and start sending message, from inside oracle

How to debug the java function that is loaded inside the oracle DB.

My custom classes are given below

public class KafkaPublisher { 
static ProducerRecord<String, String> PR = null; 
static Producer<String, String> producer = null; 
static Properties prop = new Properties(); 

public static void init() { 
prop.put("bootstrap.servers", "111.11.11.11:9092"); 
prop.put("acks", "1"); 
prop.put("retries", "0"); 
prop.put("batch.size", "16384"); 
prop.put("linger.ms", "1"); 
prop.put("buffer.memory", "33554432"); 
prop.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); 
prop.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); 

} 

public static String push(String data) { 
String response = "No responce"; 
init(); 
try { 
producer = new KafkaProducer<String, String>(prop); 
PR = new ProducerRecord<String, String>("topic1", data); 
Future<RecordMetadata> future = producer.send(PR); 
RecordMetadata rMetaData = future.get(); 
response = "Current Offset: " + rMetaData.offset(); 
} catch (Exception e) { 
response = "Message: " + e.getMessage() + " Cause: " + e.getCause(); 
} 
return response; 
} 

public static String getProperties(String msg) { 
return "My message: " + msg + prop.toString(); 
} 
}

function getProperties(String msg) is working fine but function push(String data) is not working.

1

There are 1 best solutions below

0
On

Exactly what libraries you have loaded to database? For me i have missing the slf4j-simple jar lib.

Also you need to grant to your dbuser those permissions:

exec dbms_java.grant_permission( <DB_USER>, 'SYS:javax.management.MBeanServerPermission', 'createMBeanServer', '' )
exec dbms_java.grant_permission( <DB_USER>, 'SYS:javax.management.MBeanTrustPermission', 'register', '' )
exec dbms_java.grant_permission( <DB_USER>, 'SYS:javax.management.MBeanPermission', '*', '*' )