Friday, October 10, 2008

JMS in JDeveloper 11g and WebLogic 10.3

JMS is a bit different in JDeveloper 11G and WebLogic 10.3 then when you use OC4J 10.1.3. This blog will show you how you can create a Queue and Connection Factory in WebLogic and use this in one of your JDeveloper 11g projects.
First you have to add two libraries to the project, The first is the AQJMS library ( even when we don't use AQ ) and the second library is the WebLogic 10.3 thin Client

Start the weblogic server. Menu Run , Start Server Instance


The url is http://localhost:7101/console. Username weblogic password weblogic.


Default the weblogic server has an empty configuration. We need to create a new jms server with a database of file persistance. We need this for the queue or topic persistence

create a new jms system module. In this module we will create a new connection factory and queue
Select the just created jms module and create a new connection factory first.



Create a new queue

Make sure that the queue uses the jms server. See the targets this can't be empty

And here is the java code to test the connection factory and the queue

The difference with oc4j is that you use other jndi properties. This are the right properties for WebLogic 10.3 in JDeveloper 11g

java.naming.factory.initial weblogic.jndi.WLInitialContextFactory
java.naming.provider.url t3://localhost:7101
java.naming.security.principal weblogic
java.naming.security.credentials weblogic




package nl.ordina.jms;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class WeblogicClient {

private QueueConnection connection = null;
private QueueSession session = null;
private QueueSender sender = null;
private QueueReceiver receiver = null;
private Queue queue = null;
private long waitTime = 0;


public WeblogicClient() {
setUp();
put();
get();
tearDown();
}

public static void main(String[] args) {
WeblogicClient weblogicClient = new WeblogicClient();
}

public void tearDown() {
try {
sender.close();
receiver.close();
session.close();
connection.close();

} catch (JMSException je) {
je.printStackTrace();
} finally {
}
}

public void get(){
try {
javax.jms.TextMessage textMessage = (javax.jms.TextMessage)receiver.receive();
System.out.println("Receiving message [" + textMessage.getJMSMessageID() + "] enqueued at " + new Timestamp(textMessage.getJMSTimestamp()).toString());
String xmlText = textMessage.getText();
System.out.println(xmlText);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

public void put(){
String messageId = null;
String xmlData = "";
FileInputStream fis;
try {
fis = new FileInputStream("D:\\projecten\\mhs_esb\\delfor.xml");
int x= fis.available();
byte b[]= new byte[x];
fis.read(b);
xmlData = new String(b);
} catch (FileNotFoundException e) {
// TODO
} catch (IOException e) {
// TODO
}
try {
TextMessage message = session.createTextMessage(xmlData);
sender.send(message);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

protected void setUp() {

String queueName = "jms/QTest";
String queueConnectionFactoryName = "jms/CFTest";
Context ctx;

try {
Properties parm = new Properties();
parm.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
parm.setProperty("java.naming.provider.url","t3://localhost:7101");
parm.setProperty("java.naming.security.principal","weblogic");
parm.setProperty("java.naming.security.credentials","weblogic");

ctx = new InitialContext(parm);

QueueConnectionFactory connectionFactory =
(QueueConnectionFactory)ctx.lookup(queueConnectionFactoryName);

connection = connectionFactory.createQueueConnection();
connection.start();
session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
queue = (Queue)ctx.lookup(queueName);
sender = session.createSender(queue);
receiver = session.createReceiver(queue);


} catch (JMSException je) {
throw new RuntimeException("Fout opgetreden bij het starten ",je);
} catch (Throwable t) {
throw new RuntimeException("Fout opgetreden bij het starten ",t);

}
}

}


4 comments:

Hung Huynh said...

Hi.

I have followed you example, but created a bpel process that put the msg on a topic instead of using the java code.

I get this error:

The Adapter Framework was unable to establish an outbound JCA connection due to the following issue: ORABPEL-12141
ERRJMS_CONN_FAC_NOT_FOUND.
Unable to instantiate connection factory. JMS adapter was unable to look up the connection factor jms/mdm/personCF neither through JNDI nor instantiate it as a Java class
Please examine the log file to determine the problem.

; nested exception is:
ORABPEL-12511

Do you have any suggestions?

Edwin Biemond said...

Hi

is your BPEL also installed on the wls. If it runs on OC4J then I think you don't have the wls jms libraries installed on the OC4J.

When I try to dequeue from Oracle Service Bus I use t://localhost:7001/ConnFact/QueueName as url.

Nash said...

thanks, worked on first try!

nocturne said...

Worked great, thanks!

Just two suggestions:
1- it would be great if the exception messages were written in English too.
2- the default java.naming.provider.url value uses port 7001, instead of 7101, as in the example.

I had a JMSException the first time I tried the example, but after correcting the server port, it ran smooth.

Thanks a lot!