0 Replies Latest reply on Apr 15, 2002 12:53 PM by ricrodrig

    NoInitialContextException

    ricrodrig Newbie

      Hi all,
      Here's my situation:
      I developed an MDB(ConsumerMDB) that consumes messages sent by a Sender. The Consumer runs on the Jboss server side, while the Sender acts like the client.
      On the client window the ant command runs the build.xml script and compiles all the files needed. Everything works fine, proven by the results on the Jboss window. But... while getting the XML String params on the server side, I receive (on the client window) a NoInitialContextException which seems like having no intentions to let go. Any suggestions?



      Sender.java
      ------------
      package org.jboss.docs.jms.client;
      import java.util.*;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.jms.JMSException;
      import javax.jms.Queue;
      import javax.jms.QueueConnection;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueSender;
      import javax.jms.QueueSession;
      import javax.jms.Session;
      import javax.jms.TextMessage;

      public class Sender {

      /**
      * Queue connection, must be closed.
      */
      QueueConnection queueConnection;

      /**
      * Queue session, must be closed. Used to create messages.
      */
      QueueSession queueSession;

      /**
      * Used to send messages.
      */
      QueueSender queueSender;

      /**
      * Destination to send to
      */
      Queue queue;

      /**
      * Sets up all the JMS fixtures.
      *
      * Use close() when finished with object.
      *
      * @param factoryJNDI name of the queue connection factory to look up.
      * @param queueJNDI name of the queue destination to look up.
      */
      public Sender(String factoryJNDI, String queueJNDI) throws JMSException, NamingException {

      // Get the initial context
      Hashtable prop = new Hashtable();
      prop.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
      prop.put("java.naming.factory.url","172.1.3.109:1099");
      prop.put("java.naming.rmi.security.manager", "no");
      prop.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
      prop.put(Context.PROVIDER_URL, "172.1.3.109:1099");

      Context context = new InitialContext(prop);

      // Get the connection factory
      QueueConnectionFactory queueFactory = (QueueConnectionFactory)context.lookup(factoryJNDI);

      // Create the connection
      queueConnection = queueFactory.createQueueConnection();

      // Create the session
      queueSession = queueConnection.createQueueSession(
      // No transaction
      false,
      // Auto acknowledge
      Session.AUTO_ACKNOWLEDGE);

      // Look up the destination
      queue = (Queue)context.lookup(queueJNDI);

      // Create a sender
      queueSender = queueSession.createSender(queue);
      }

      /**
      * Send the given String as a JMS message to the testQueue queue.
      */
      public void send(String sXML) throws JMSException {

      // Create a message
      TextMessage message = queueSession.createTextMessage();
      message.setText(sXML);

      // Send the message
      queueSender.send(queue, message);
      }

      /**
      * Close session and connection. When done, no sending is possible any more.
      */
      public void close() throws JMSException {
      queueSession.close();
      queueConnection.close();
      }

      /**
      * Create Sender, send XML String to testQueue and close down sender.
      */
      public static void main(String[] args) {
      try {

      // Create the Sender, giving it the name of the QueueConnection
      // Factory and the Queue destination to use in lookup.
      Sender sender = new Sender(

      // Name of ConnectionFactory
      "QueueConnectionFactory",
      // Name of destination to send to
      "queue/testQueue");

      String sXML = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?><request client = \"AC188\" action = \"CPM_TKOV\"><parameter name = (...)";
      sender.send(sXML);

      // Close down sender
      sender.close();
      }
      catch(Exception ex) {
      System.out.println("An exception occured while testing Sender: " + ex);
      ex.printStackTrace();
      }
      }
      } // Sender

      ------------------------------------------

      ConsumerMDB.java
      -----------------
      package org.jboss.docs.jms.mdb.bean;
      import java.util.*;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.MessageDrivenBean;
      import javax.ejb.MessageDrivenContext;
      import javax.naming.InitialContext;
      import javax.naming.Context;
      import javax.naming.NoInitialContextException;
      import javax.rmi.PortableRemoteObject;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.xml.bind.*;
      import javax.xml.bind.MarshallableRootElement;
      import javax.xml.marshal.*;
      import com.accenture.connectors.parsers.xml.*;
      import com.accenture.mediator.*;

      public class ConsumerMDB implements MessageDrivenBean, MessageListener {

      /** Internal MessageDrivenContext.
      */
      private MessageDrivenContext ctx = null;

      /** No argument constructor required by container.
      */
      public ConsumerMDB() {
      }

      /** Sets internal MessageDrivenContext variable.
      * @see javax.ejb.MessageDrivenBean#setContext(javax.ejb.MessageDrivenContext)
      * @param ctx The context to override internal empty one
      */
      public void setMessageDrivenContext(MessageDrivenContext cx) throws EJBException {
      this.ctx = cx;
      }

      /** Empty ejbCreate method.
      *
      */
      public void ejbCreate() {
      }

      /** Empty ejbActivate method.
      *
      */
      public void ejbActivate() {
      }

      /** Empty ejbPassivate method.
      *
      */
      public void ejbPassivate() {
      }

      /** Empty ejbRemove method.
      *
      */
      public void ejbRemove() {
      }

      /** MessageListener used to receive asynchronously delivered messages.
      * Each session must insure that it passes messages serially to the listener.
      * This means that a listener assigned to one consumer of the same session
      * can assume that the onMessage method is not called with the next message until
      * the session has completed the last call.
      * @param message the message passed to the listener.
      */
      public void onMessage(Message message) {

      ObjectRequest objReq = new ObjectRequest();
      XMLWrapper wrapper = new XMLWrapper();
      MediatorHome medHome = null;

      try {
      String str = message.toString();
      System.out.println("str: " + str);
      String sub = str.substring(str.indexOf("@")+1, str.length());
      System.out.println("sub: " + sub);

      objReq = wrapper.execRequest(sub);


      // Get a naming context
      InitialContext jndiContext = new InitialContext();

      // Get a reference to the Mediator Bean
      Object ref = jndiContext.lookup("Mediator");

      // Get a reference from this to the Mediator Bean's Home interface
      medHome = (MediatorHome) PortableRemoteObject.narrow(ref, MediatorHome.class);

      // Create a Mediator object from the Mediator Home interface
      Mediator bean = medHome.create();

      if(objReq != null) {
      //Call invokeMethod() of Mediator and pass the client's request
      objReq = bean.execute(objReq);
      }

      System.out.println("params " + objReq.getParams().toString());
      }
      catch(NoInitialContextException ex) {
      System.out.println("InitialContextException: " + ex.getExplanation());
      }
      catch(Exception ex) {
      System.out.println("Exception: " + ex.getMessage());
      }
      }
      } // ConsumerMDB


      Thanx in advance,
      ricrodrig