Monday 18 July 2011

Weblogic Server JMS message tester

I needed a way to send test messages through the CLI to Weblogic Server JMS modules (on server to test things were going as expected). I hacked together the following java code:
import javax.jms.*;
import javax.naming.*;
import java.util.Properties;
import java.io.*;

public class JMSSendMessage {

    public static void main(String[] args) throws Exception {
 if (args.length != 6) {
     System.out.println ("Usage: TOPIC|QUEUE ProviderURL UserName Password DestName FileName");
     return;
 }

 String destType = args[0];
 String provider = args[1];
 String username = args[2];
 String password = args[3];
 String destName = args[4];
 String filePath = args[5]; 

 if (!(destType.equals("QUEUE") || destType.equals("TOPIC"))) 
            throw new Exception ("Must specify destination as TOPIC or QUEUE");

 Properties props = System.getProperties();
 props.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
 props.put(Context.PROVIDER_URL, provider);
 props.put(Context.SECURITY_PRINCIPAL, username);
 props.put(Context.SECURITY_CREDENTIALS, password);

 InitialContext ctx = new InitialContext(props);

 Session session = null;
 MessageProducer producer = null;
 Connection connection = null;

 if (destType.equals("TOPIC")) {
     TopicConnectionFactory tconfactory = 
            (TopicConnectionFactory)ctx.lookup("javax.jms.TopicConnectionFactory");
     TopicConnection tcon = tconfactory.createTopicConnection();
     TopicSession tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
     Topic topic = (Topic)ctx.lookup(destName);
     TopicPublisher tpublisher = tsession.createPublisher(topic);
     session = (Session)tsession;
     producer = (MessageProducer)tpublisher;
     connection = (Connection)tcon;
 } else if (destType.equals("QUEUE")) {
     QueueConnectionFactory qconfactory = 
            (QueueConnectionFactory)ctx.lookup("javax.jms.QueueConnectionFactory");
     QueueConnection qcon = qconfactory.createQueueConnection();
     QueueSession qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
     Queue queue = (Queue)ctx.lookup(destName);
     QueueSender qsender = qsession.createSender(queue);
     session = (Session)qsession;
     producer = (MessageProducer)qsender;
     connection = (Connection)qcon;
 }

 TextMessage msg = session.createTextMessage();

 connection.start();
 byte[] buffer = new byte[(int)new File(filePath).length()];
 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
 bis.read(buffer);
 bis.close();

 String textOutput = new String(buffer);

 System.out.println("Sending the following to: " + destName + 
             " (" + destType + ") on provider: " + provider);
 System.out.println(textOutput);

 msg.setText(textOutput);
 if (producer instanceof TopicPublisher) ((TopicPublisher)producer).publish(msg);
 else if (producer instanceof QueueSender) ((QueueSender)producer).send(msg);
 
 producer.close();
 session.close();
 connection.close();

 System.out.println("File sent successfully");

    }

}

You need two WLS files to compile it: wlclient.jar, wljmsclient.jar which you can 'borrow' from the WLS install directory.

No comments:

Post a Comment