A Java Client to produce messages in the WLS JMS Server running on OCI

In the previous post I setup a JMS server on WLS running on OCI. In this one the goal is to use a Java Client to produce messages into the jms server queue.

The access to the JMS server I will use the WebLogic Thin T3 Client as described here.

For this to work we need to call the t3 protocol from the same network.

As stated in the above screenshot , we will access the T3 application from within the same VCN.

WLS on K8S ?

In the case one has WLS running on OCI but deployed with K8S, then there are other requirements as described in this post -> https://blogs.oracle.com/weblogicserver/t3-rmi-communication-for-weblogic-server-running-on-kubernetes

Java Sender

I used the code (with minor changes) from an A-team post: https://www.ateam-oracle.com/a-simple-jms-client-for-weblogic-11g . You need to update some properties – QUEUE_NAME, QCF_NAME,PROVIDER_URL , SECURITY_PRINCIPAL and SECURITY_CREDENTIALS.

package jms.queue;

import java.util.Hashtable; 
import javax.naming.*; 
import javax.jms.*;

 public class JMSSender {   
	 
	 private static InitialContext ctx = null;    
	 private static QueueConnectionFactory qcf = null;    
	 private static QueueConnection qc = null;    
	 private static QueueSession qsess = null;    
	 private static Queue q = null;   
	 private static QueueSender qsndr = null;    
	 private static TextMessage message = null;    
	 
	 // NOTE: The next two lines set the name of the Queue Connection Factory    //       and the Queue that we want to use.    
	 
	 private static final String QCF_NAME = "jms/ConnectionFactory";   
	 private static final String QUEUE_NAME = "jms/queue1";    
	 
	 public JMSSender() {        super();    }    
	 
	 public static void sendMessage(String messageText) {       
		 // create InitialContext       
		 Hashtable<String, String> properties = new Hashtable<String, String>();        
		 properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");       
		 // NOTE: The port number of the server is provided in the next line,        
		 //       followed by the userid and password on the next two lines.       
		 properties.put(Context.PROVIDER_URL, "t3://<ipaddress>:9073");        
		 properties.put(Context.SECURITY_PRINCIPAL, "weblogic");        
		 properties.put(Context.SECURITY_CREDENTIALS, "mypassword");        
		 
		 try {    
			 System.out.println("Trying InitialContext ");        
			 ctx = new InitialContext(properties);        
			 } catch (NamingException ne) 
		 {            ne.printStackTrace(System.err);           
		 System.exit(0);        }        
		 System.out.println("Got InitialContext " + ctx.toString());        
		 // create QueueConnectionFactory        
		 try {            qcf = (QueueConnectionFactory)ctx.lookup(QCF_NAME);        
		 }        catch (NamingException ne) 
		 {            ne.printStackTrace(System.err);            
		 System.exit(0);        }        
		 System.out.println("Got QueueConnectionFactory " + qcf.toString());      
		 
		 // create QueueConnection       
		 try {            
			 qc = qcf.createQueueConnection();       
			 }        catch (JMSException jmse)
		 {            jmse.printStackTrace(System.err);            
		 System.exit(0);        }       
		 System.out.println("Got QueueConnection " + qc.toString());        
		 
		 // create QueueSession        
		 try {            qsess = qc.createQueueSession(false, 0);        
		 }        catch (JMSException jmse) 
		 {            jmse.printStackTrace(System.err);           
		 System.exit(0);        }        
		 System.out.println("Got QueueSession " + qsess.toString());       
		 
		 // lookup Queue      
		 try {            q = (Queue) ctx.lookup(QUEUE_NAME);       
		 }        catch (NamingException ne) {          
			 ne.printStackTrace(System.err);          
			 System.exit(0);        }        
		 System.out.println("Got Queue " + q.toString());     
		 
		 // create QueueSender       
		 try {            qsndr = qsess.createSender(q);     
		 }        catch (JMSException jmse) {          
			 jmse.printStackTrace(System.err);        
			 System.exit(0);        }        
		 System.out.println("Got QueueSender " + qsndr.toString());     
		 
		 // create TextMessage      
		 try {            message = qsess.createTextMessage();   
		 }        catch (JMSException jmse) {        
			 jmse.printStackTrace(System.err);          
			 System.exit(0);        }        
		 System.out.println("Got TextMessage " + message.toString());      
		 
		 // set message text in TextMessage       
		 try {            message.setText(messageText);     
		 }        catch (JMSException jmse) {         
			 jmse.printStackTrace(System.err);      
			 System.exit(0);        }       
		 System.out.println("Set text in TextMessage " + message.toString());    
		 
		 // send message        
		 try {            qsndr.send(message);      
		 }        catch (JMSException jmse) {         
			 jmse.printStackTrace(System.err);         
			 System.exit(0);        }       
		 System.out.println("Sent message ");     
		 
		 // clean up        
		 try {          
			 message = null;      
			 qsndr.close();  
			 qsndr = null;  
			 q = null;        
			 qsess.close();   
			 qsess = null;    
			 qc.close();      
			 qc = null;       
			 qcf = null;      
			 ctx = null;      
			 }        catch (JMSException jmse)
		 {            jmse.printStackTrace(System.err);      
		 }        System.out.println("Cleaned up and done.");   
		 }   
	 public static void main(String args[]) {        sendMessage("JMS Client with WLS on OCI");    } 
	 }
	
	
	

Jar Dependencies

There is one required library (wlthint3client) that replaces the old wlclient and the wljmsclient jar files. The library can be found in the below directory.

  • <WebLogic_Directory>\server\lib\wlthint3client.jar

In the below screenshot you can see the 3 libraries, but only the wlthint3client.jar is required!!

Putting all to Test

I could call this java program from command line, however for testing purposes its easier to use the engine from Eclipse.

Under Services>Messaging>JMS Modules>Queue>Monitoring we can see the messages and it’s details.

Other JMS Headers

There are many extensions to the base JMS specifications and we can easily choose headers and properties that deliver extra information. I will leave this for another post.

Final Note

JMS and Java consumers sound like old news, but there are still plenty of applications and systems using this architecture. It’s solid, it’s proven and flexible.