How do you implement a client that sends messages via a queue?

Jerry Smith

Client implementations vary with respect to set-up operations, depending on the JMS implementation. The following client, ObjectSender, works with the WebLogic Java application server, which supports JMS:

import javax.jms.*;
import java.io.Serializable;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ObjectSender extends Thread {
  public static final String SERVER_URL = "t3://localhost:7001";
  public static final String JMS_FACTORY =
    "javax.jms.QueueConnectionFactory";
  public static final String QUEUE = "j2ee.jms.objectQueue";

  private Context c;
  private QueueConnectionFactory qcf;
  private QueueConnection con;
  private QueueSession session;
  private QueueSender sender;
  private Queue queue;
  private ObjectMessage msg;
  private boolean readyToSend;

  public static void main(String[] args) {
    if (!checkUsage(args))
      System.exit(-1);
    ObjectSender os =
      (args.length == 1) ?
        new ObjectSender(args[0]) :
        new ObjectSender();
    if (!os.isReadyToSend()) {
      System.out.println("Not ready to send messages.");
      System.exit(-1);
    }
    os.sendObject(new Cat());
    os.sendObject(new Dog());
    os.sendObject(null);
    os.close();
  }

  public ObjectSender() {
    this(SERVER_URL);
  }

  public ObjectSender(String url) {
    readyToSend = true;
    c = getInitialContextWebLogic(url);
    if (c == null)
      readyToSend = false;
    else
      init();
  }

  public void init() {
    if (!readyToSend)
      return;
    try {
      qcf = (QueueConnectionFactory) c.lookup(JMS_FACTORY);
      con = qcf.createQueueConnection();
      session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    }
    catch (Exception e) {
      readyToSend = false;
      System.out.println("Can't initialize time sending.");
      closeContext();
      return;
    }
    try {
      queue = (Queue) c.lookup(QUEUE);
    }
    catch (NamingException ne) {
      try {
        queue = session.createQueue(QUEUE);
        c.bind(QUEUE, queue);
      }
      catch (Exception e) {
        readyToSend = false;
        closeConnection();
        return;
      }
    }
    try {
      sender = session.createSender(queue);
      msg = session.createObjectMessage();
      con.start();
    }
    catch (Exception e) {
      readyToSend = false;
      closeConnection();
      return;
    }
  }

  public void close() {
    if (!readyToSend)
      return;
    try {
      sender.close();
      closeConnection();
    }
    catch (Exception e) {
      System.out.println("Can't close ObjectSender instance.");
    }
  }

  private void closeConnection() {
    if (!readyToSend)
      return;
    try {
      session.close();
      con.close();
      closeContext();
    }
    catch (Exception e) {
      System.out.println("Can't close connection.");
    }
  }

  private void closeContext() {
    if (!readyToSend)
      return;
    try {
      c.close();
    }
    catch (Exception e) {
      System.out.println("Can't close JNDI context.");
    }
  }

  public void sendObject(Serializable obj) {
    if (!readyToSend)
      return;
    try {
      msg.setObject(obj);
      sender.send(msg);
    }
    catch (Exception e) {
      System.out.println("Can't send message: " + e);
    }
  }

  public boolean isReadyToSend() {
    return readyToSend;
  }

  private Context getInitialContextWebLogic(String url) {
    try {
      Hashtable h = new Hashtable();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      h.put("weblogic.jndi.createIntermediateContexts", "true");
      return new InitialContext(h);
    }
    catch (Exception e) {
      System.out.println("Can't get JNDI context.");
      return null;
    }
  }

  public static boolean checkUsage(String[] args) {
    if (args.length > 1) {
      System.out.println("Usage:  java ObjectSender [<server-url>]");
      return false;
    }
    return true;
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact