<br>Hi<br>Please follow below given steps for using concept of calling java class from Oracle 11.1 Db.<br>Step 1: Before using concept of calling java from Oracle Function/Store Procedure or Trigger check properly that oracle contains inbuilt JVM for the same.<br>We can use following given query to check the same.<br>select status, count(status) from all_objects where object_type='JAVA CLASS' group by status;<br>if the output of above query is greater than 0 (zero) i.e. in most case count(status) will be greater than 10,000 and status should be Valid<br>If the above query doesn’t work indicate that JAVA is not present in Oracle<br>Please run following command $ORACLE_HOME/javavm/install/initjvm (this will install java in Oracle). Do validate again from above query for proper installation.<br>Step 2: Inbuild java of Oracle provide loadjava command to perform operation of using JAVA Class from Oracle Function/Store Procedure or Trigger.<br>There are two ways to use this command.<br><br>Ø By using Oracle SQL Developer loadjava GUI functionality<br>Ø By using loadjava command from Command Prompt.<br>· By using Oracle SQL Developer loadjava GUI functionality:<br><br>Once the java in installed in Oracle we can find Java option in hierarchy tree of Oracle SQL Developer.<br>Right click on the java and select Load Java as shown below and fill the Java Source Name with the name of the Java Class and paste the java code inside text area. Submit the form using Apply button.<br><br>After successful addition of the java class we will be able to see TestReturnString class inside the Tree Structrure under JAVA folder of Oracle SQL Developer. Confirm the Status of that loaded class into oracle using following sql script.<br>select owner, object_name, status from all_objects where object_name like '% TestReturnString %' and object_type like 'JAVA%';<br>This will give Status as Valid. In case of InValid status we can see the error by using following sql script<br>SELECT name, type, sequence, substr(text,0,100) FROM user_errors where name like '% TestReturnString %';<br>Remove all the error as stated by the above sql and run the following script to chagne the status of the class from InValid to Valid.<br>ALTER JAVA class "TestString" resolve;<br>Finally create a Oracle Function or Procedure to check the loaded java classe execution<br>I.e.<br>create or replace<br>FUNCTION pMeProcFunction (<br>message VARCHAR2<br>)<br>RETURN STRING AS LANGUAGE JAVA<br>NAME 'TestReturnString.getMessage(java.lang.String) return java.lang.String';<br>==Java Class<br>public class TestReturnString {<br>public static String getMessage(String strHello)<br>{<br>return strHello;<br>}<br>}<br>=========Execution<br>select pmeprocfunction('test') from dual;<br>· By using loadjava command from Command Prompt:<br>This is the proper way of using loadjava functionality or oracle. As it allows end user to understand the complete flow of the loadjava<br>Before using loadjava command on prompt concept we need to take care of following below given notes: (In our case as we had our Oracle on Linux M/C)<br>1) Set PATH,CLASSPATH,ORACLE_HOME variable. Better to us bash_profile of linux<br>i.e. vi ~/.bash_profile<br>export PATH=$PATH:$HOME/bin:/app/jdk1.6.0_06/bin<br>export CLASSPATH=.:/app/jdk1.6.0_06<br>export ORACLE_HOME=/app/oracle/base/<br>#export PATH<br>unset USERNAME<br>2) Most important point to follow is to set all the class path needed for loadjava command using inbuild helloworld java example by Oracle J.<br>In our case move to<br>[root@linux-ent1 /]# cd app/oracle/base/javavm/demo/examples/jsproc/basic/helloWorld/<br>and run make command to set all the call path<br>3) Use the same prompt to upload the class in oracle using following given command better to put only class file on linux m/C<br>/app/oracle/base/bin/loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/bin/com/dbMQTrigger/TestReturnString.class<br>And test the above deployed java class using procedure/function of Oracle.<br>Now let discuss on real Business ……………… HOW TO PERFORM OPERATION ON RABBITMQ WHEN A TABLE GET UPDATED.<br>For above concept we had used following business model<br>Db Table à On update/delete/Insert will fire à Trigger à Trigger will call Stored Procedure à Stored Procedure will call Java Class à Finally JAVA Class will insert message into Rabbit MQ.<br>Now as stated above we are using Rabbit MQ as messaging bus the class which is used to insert data into Rabbit MQ contain imports some of the java class which need RabbitMQ inbuilt jar to be loaded first into the JVM of the Oracle. Here comes the real picture of using concept step2 where we need to use loadjava on command prompt, as Oracle SQL Developer and TOAD does not support loading of JAVA jar file like class file.<br>Approach taken was as follows:<br>1) First load all the jar files which is need by our class that is used to insert message in Rabbit Mq i.e. commons-io-1.2.jar, rabbitmq-client.jar, rabbitmq-client-tests.jar and commons-cli-1.1.jar<br>Using following command<br>/app/oracle/base/ bin/loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/lib/commons-cli-1.1.jar<br>/app/oracle/base/ bin/loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/lib/commons-io-1.2.jar<br>/app/oracle/base/bin/loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/lib/rabbitmq-client.jar<br>/app/oracle/base/ bin/ loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/lib/rabbitmq-client-tests.jar<br>2) Finally load our java class which actually insert data into RabbitMQ.<br>/app/oracle/base/ bin/loadjava -f -verbose -oracleresolver -resolve -oci8 -u userid/pwd@SIDNAME /Siddhu/RabbitMQTrigger/bin/com/dbMQTrigger/RMQNOPARAM.class<br>Or use Oracle GUI loadjava tool for the same.<br>Finally write the following Trigger/Procedure and execute the see all working properly:<br>===Trigger<br>create or replace<br>TRIGGER TESTTR<br>AFTER INSERT OR DELETE OR UPDATE ON TEST_RABBITMQ<br>--REFERENCING OLD AS OLD NEW AS NEW<br>FOR EACH ROW<br>BEGIN<br>dbms_output.put_line('Trigger Start');<br>-- insert into TESTTRIGGER values ('first');<br>--pMeProc('hi');<br>pMeProc();<br>-- insert into TESTTRIGGER values ('second');<br>dbms_output.put_line('Trigger End');<br>--commit;<br>END;<br>===Procedure<br>create or replace<br>PROCEDURE pMeProc<br>AS LANGUAGE JAVA<br>NAME 'RMQNOPARAM.publishMessage()';<br>==JAVA Class<br><code><br>import com.rabbitmq.client.Channel;<br>import com.rabbitmq.client.Connection;<br>import com.rabbitmq.client.ConnectionFactory;<br>public class RMQNOPARAM {<br>public static void publishMessage()<br>{<br>try<br>{<br>Connection conn = null;<br>ConnectionFactory factory = new ConnectionFactory();<br>factory.setHost("");<br>conn = factory.newConnection();<br>Channel chan = conn.createChannel();<br>String exchangeName = "TestDbTriggerRabbitMQExchange";<br>String routingKey = "TestDbTriggerRabbitMQRoutingKey";<br>//System.out.println(" Value of strHello is:"+strHello);<br>chan.basicPublish(exchangeName, routingKey, null, "fromjava".getBytes());<br>//System.out.println(" Published Message 'Hello World!'");<br>chan.close();<br>conn.close();<br>}catch(Exception e)<br>{<br>e.printStackTrace();<br>}<br>}<br>}<br></code><br>==sql command<br><br>insert into test_rabbitMQ values ('helloTrigger')<br><br>