2 Replies Latest reply on Jul 9, 2004 12:22 PM by boo

    Problem during Topic lookup

    boo

      Hi,

      Finally I post to this forum since I can not find a solution for my (perhaps minor) problem anywhere else.
      I try to write a Session Bean that should send a JMS message by using the TopicConnectionFactory called 'ConnectionFactory' and a topic called 'MyTopic'.

      I get the following Exception:

      12:48:17,411 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract void server.log.interfaces.LogMessageService.sendMessage(java.lang.String) throws java.rmi.RemoteException, causedBy:
      javax.naming.NameNotFoundException: MyTopic not bound
       at org.jnp.server.NamingServer.getBinding(NamingServer.java:495)
       at org.jnp.server.NamingServer.getBinding(NamingServer.java:503)
       at org.jnp.server.NamingServer.getObject(NamingServer.java:509)
       at org.jnp.server.NamingServer.lookup(NamingServer.java:282)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:528)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:507)
       at javax.naming.InitialContext.lookup(InitialContext.java:347)
       at server.log.LogMessageServiceEJB.sendMessage(LogMessageServiceEJB.java:53)
      

      My code looks like this:
      /**
       * @ejb.bean
       * name="LogMessageService"
       * jndi-name="LogMessageService"
       * type="Stateless"
       * view-type="remote"
       *
       * @ejb.transaction type="Required"
       *
       * @ejb.permission
       * role-name = "Authenticated"
       *
       * @ejb.resource-ref res-ref-name = "jms/TopicConnectionFactory" res-type = "javax.jms.TopicConnectionFactory" res-auth = "Container"
       * @jboss.resource-ref res-ref-name = "jms/TopicConnectionFactory" jndi-name = "ConnectionFactory"
       *
       * @ejb.resource-env-ref name = "MyTopic" type = "javax.jms.Topic"
       */
      public class LogMessageServiceEJB implements SessionBean {
      
       /**
       * @ejb.create-method
       */
       public void ejbCreate() throws javax.ejb.CreateException {
       }
      
       /**
       * @ejb.interface-method
       */
       public void sendMessage( String text) throws RemoteException {
       System.out.println("LogMessageServiceEJB.sendMessage(): text="+text);
       try {
       Context initialContext = new InitialContext();
       TopicConnectionFactory f = ( TopicConnectionFactory ) initialContext.lookup( "java:comp/env/jms/TopicConnectionFactory" );
      // Topic myTopic = ( Topic ) initialContext.lookup( "java:comp/env/MyTopic" );
       Topic myTopic = ( Topic ) initialContext.lookup( "MyTopic" );
      
       TopicConnection topicConnection = f.createTopicConnection();
       TopicSession topicSession = topicConnection.createTopicSession( true, 0);
      
       TopicPublisher topicPublisher = topicSession.createPublisher( myTopic );
      
       TextMessage message = topicSession.createTextMessage( text);
       topicPublisher.publish( message);
       topicConnection.close();
      
       } catch ( NamingException ex ) {
       throw new EJBException( ex );
       } catch ( JMSException ex ) {
       throw new EJBException( ex );
       }
       }
      }
      

      Even when I lookup the topic with
      Topic myTopic = ( Topic ) initialContext.lookup( "java:comp/env/MyTopic" );
      
      I get the following Exception.
      13:09:49,171 ERROR [LogInterceptor] TransactionRolledbackException in method: public abstract void server.log.interfaces.LogMessageService.sendMessage(java.lang.String) throws java.rmi.RemoteException, causedBy:
      javax.naming.NamingException: Could not dereference object [Root exception is java.lang.NullPointerException]
       at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:970)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:613)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:642)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:507)
       at javax.naming.InitialContext.lookup(InitialContext.java:347)
       at server.log.LogMessageServiceEJB.sendMessage(LogMessageServiceEJB.java:51)
      


      In order to 'enable' the topic 'MyTopic' I place a file called 'myjms-service.xml' into the jboss/server/default/deploy/ directory with the following content:
      <?xml version="1.0" encoding="UTF-8"?>
      
      <server>
       <mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=MyTopic">
       <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
       <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
       <attribute name="SecurityConf">
       <security>
       <role name="guest" read="true" write="true"/>
       <role name="Authenticated" read="true" write="true" create="true"/>
       </security>
       </attribute>
       </mbean>
      </server>
      </pre>
      


      I don't know what is wrong. I would appreciate any hints.

      Boo.

        • 1. Re: Problem during Topic lookup
          boo

          Ok, JBoss prepends the string 'topic/' to the topic name. So the real JNDI name of the topic 'MyTopic' is 'topic/MyTopic'.

          Adding the xdoclet class level tag

          @jboss.resource-env-ref resource-env-ref-name = "MyTopic" jndi-name = "topic/MyTopic"
          

          to LogMessageServiceEJB has fixed this problem.

          Cheers,

          Boo.

          • 2. Re: Problem during Topic lookup
            boo

            If anybody is interested - here is the working code:

            /**
             * @ejb.bean
             * name="LogMessageService"
             * jndi-name="LogMessageService"
             * type="Stateless"
             * view-type="remote"
             *
             * @ejb.transaction type="Required"
             *
             * @ejb.permission
             * role-name = "Authenticated"
             *
             * @ejb.resource-ref res-ref-name = "jms/TopicConnectionFactory" res-type = "javax.jms.TopicConnectionFactory" res-auth = "Container"
             * @jboss.resource-ref res-ref-name = "jms/TopicConnectionFactory" jndi-name = "java:/JmsXA"
             *
             * @ejb.resource-env-ref name = "MyTopic" type = "javax.jms.Topic"
             * @jboss.resource-env-ref resource-env-ref-name = "MyTopic" jndi-name = "topic/MyTopic"
             */
            public class LogMessageServiceEJB extends AbstractServiceEJB implements SessionBean {
            
             /**
             * @ejb.create-method
             */
             public void ejbCreate() throws javax.ejb.CreateException {
             }
            
             /**
             * @ejb.interface-method
             */
             public void sendMessage( String text) throws RemoteException {
             System.out.println("LogMessageServiceEJB.sendMessage(): text="+text);
             TopicConnection topicConnection = null;
             TopicSession topicSession = null;
             try {
             Context initialContext = new InitialContext();
             TopicConnectionFactory f = ( TopicConnectionFactory ) initialContext.lookup( "java:comp/env/jms/TopicConnectionFactory" );
             Topic myTopic = ( Topic ) initialContext.lookup( "java:comp/env/MyTopic" );
            
            
             topicConnection = f.createTopicConnection();
             topicSession = topicConnection.createTopicSession( false, 0);
            
             TopicPublisher topicPublisher = topicSession.createPublisher( myTopic );
            
             TextMessage message = topicSession.createTextMessage( text);
             topicPublisher.publish( message);
            
            
             } catch ( NamingException ex ) {
             throw new EJBException( ex );
             } catch ( JMSException ex ) {
             throw new EJBException( ex );
             } finally {
             try {
             if ( topicSession != null ) {
             topicSession.close();
             }
             } catch ( JMSException ex1 ) {
             System.err.println( ex1.getMessage());
             ex1.printStackTrace();
             }
             try {
             if ( topicConnection != null ) {
             topicConnection.close();
             }
             } catch ( JMSException ex1 ) {
             System.err.println( ex1.getMessage());
             ex1.printStackTrace();
             }
             }
             }
            
            }
            


            Boo.