2 Replies Latest reply on Jul 1, 2009 2:21 PM by Alexander Lengson

    Inject dependency on JMS topic into servlet on startup

    Alexander Lengson Newbie

      Hello All,

      I have EAR with multiple EJB and single WAR inside.
      EJBs inject JMS administrative resources such as Destination and ConnectionFactory using @Resource annotation.
      They depend on the MBean which represents JMS Topic. MBean is configured in deployment descriptor within one of EJB and that works well.
      But when I try to apply the same dependency for servlet code (servlet is marked as load-on-startup in web.xml) and deploy EAR onto JBoss, it fails.
      This is happening (as I realized from logs) because JMS Topic has not been bound yet.
      If I don't mark servlet as load-on-startup, but rather invoke it after server has started, injection in servlet works well.

      What is the right way to specify that servlet does depend on the JMS resource (or MBean which represents this resource)?

      Exception:

      2009-06-29 16:46:46,218 INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/htc
      2009-06-29 16:46:46,734 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/htc]] (HDScanner) Marking servlet EventListenerServlet as unavailable
      2009-06-29 16:46:46,750 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/htc]] (HDScanner) Servlet /htc threw load() exception
      javax.naming.NameNotFoundException: topic not bound
       at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
       at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
       at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
       at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
       at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
       at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
       at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
      

      Servlet code:
      @Depends({"jboss.mq.destination:service=Topic,name=NMSTopic"})
      public class EventListenerServlet extends HttpServlet
      {
       @Resource(name = "NMSTopic", mappedName = "jms/topic")
       private Destination destination;
      
       @Resource(name = "ConnectionFactory", mappedName = "java:ConnectionFactory")
       private ConnectionFactory connectionFactory;
      
       private javax.jms.Connection jmsConnection;
       private Session session = null;
      ...
      


      EJB code:
      @Stateful
      @Depends("jboss.mq.destination:service=Topic,name=NMSTopic")
      public class NMS implements NMSRemote
      {
       private transient Logger logger = Logger.getLogger(NMS.class);
      
       @Resource(name = "NMSTopic", mappedName = "jms/topic")
       private Destination destination;
      
       @Resource(name = "ConnectionFactory", mappedName = "java:ConnectionFactory")
       private ConnectionFactory connectionFactory;
      ...

      Deployment descriptor for that bean: (META-INF\jbossmq-destinations-service.xml)
      <?xml version="1.0" encoding="UTF-8"?>
      <server>
       <mbean code="org.jboss.mq.server.jmx.Topic"
       name="jboss.mq.destination:service=Topic,name=NMSTopic">
       <attribute name="JNDIName">jms/topic</attribute>
       <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
       </mbean>
      </server>
      



      Thanks,
      Alex.