Injecting a jms resource into a servlet and best practices for MDB

using ejb 3.1, servlet 3.0 (Glassfish v3 server)

Scenario: I have an MDB that listens for jms messages and handles some other bean (Stateless) sessions. Jms injection resource.

Question 1: Why can't Servlet inject jms resources when using static declaration?

@Resource(mappedName = "jms/Tarturus")
private static ConnectionFactory connectionFactory;

@Resource(mappedName = "jms/StyxMDB")
private static Queue queue;


private Connection connection;

      

and

@PostConstruct
    public void postConstruct() {
        try {
            connection = connectionFactory.createConnection();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void preDestroy() {
        try {
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

      

The error I am getting:

[# | 2010-05-03T15: 18: 17,118 + 0300 | WARNING | glassfish3.0 | javax.enterprise.system.container.web.com.sun.enterprise.web | _ThreadID = 35; _ThreadName = threaded 1; | StandardWrapperValve [WorkerServlet]: PWC1382: Throw an exception for the WorkerServlet com.sun.enterprise.container.common.spi.util.InjectionException: Failed to create managed object for class ua.co.rufous.server.services.WorkerServiceImpl in com.sun. enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject (InjectionManagerImpl.java:312) at com.sun.enterprise.web.WebContainer.createServletInstance (WebContainer.java:709) at com.sun.enterprise.web.WB. createServletInstance (WebModule.java:1937) in org.apache.catalina.core.StandardWrapper.loadServlet (StandardWrapper.java:1252) Called: com.sun.enterprise.container.common.spi.util.InjectionException: Exception trying to inject Unresolved Message-Destination-Ref ua.co.rufous.server.services.WorkerServiceImpl / queue @ java.lang.String @ null into ua.co.rufous.server.services.WorkerServiceImpl class in com.sun .enterprise.container.common.impl.util.InjectionManagerImpl._inject (InjectionManagerImpl.java:614) at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject (InjectionManagerImpl.java:384) at com.sun .enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance (InjectionManagerImpl.java:141) at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance (InjectionManagerImpl.java:127) at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance (InjectionManagerImpl.java:127) .enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject (InjectionManagerImpl.java:306) ... 27 more Reasons: com.sun.enterprise.container.common.spi.util.InjectionException: Illegal use of personal static field static javax.jms.Queue ua.co.rufous.server.services.WorkerServiceImpl.queue class that only supports injection based on com.sun.enterprise.container.common.impl.util. InjectionManagerImpl._inject (InjectionManagerImpl.java:532) ... 31 more | #]

my MDB:

/**
 * asadmin commands
 * asadmin create-jms-resource --restype javax.jms.ConnectionFactory jms/Tarturus
 * asadmin create-jms-resource --restype javax.jms.Queue jms/StyxMDB
 * asadmin list-jms-resources
 */
@MessageDriven(mappedName = "jms/StyxMDB", activationConfig =
{
    @ActivationConfigProperty(propertyName = "connectionFactoryJndiName", propertyValue = "jms/Tarturus"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})

public class StyxMDB implements MessageListener {

    @EJB
    private ActivationProcessingLocal aProcessing;

    public StyxMDB() {
   }

    public void onMessage(Message message) {
        try {
            TextMessage msg = (TextMessage) message;
            String hash = msg.getText();
            GluttonyLogger.getInstance().writeInfoLog("geted jms message hash = " + hash);
        } catch (JMSException e) {

        }

    }


}

      

everything works well without static declaration:

@Resource(mappedName = "jms/Tarturus")
    private ConnectionFactory connectionFactory;

    @Resource(mappedName = "jms/StyxMDB")
    private Queue queue;

    private Connection connection;

      

Question 2: Which is best for working with MDB: handling the full request in onMessage (), or calling another bean (stateless bean in my case) in the onMessage () method that would handle it. Processing involving multiple calls to soap services, so the total processing time can be 3 seconds.

Thanks.

+2


a source to share


1 answer


Answers:
1. You cannot enter a resource into a static field. Injection into member fields occurs when constructing an object, static fields are not part of the object (only part of the class). Also, EJBs and servlets are streaming objects, so it can be dangerous to do so.
2. If splitting processing across multiple EJBs makes sense, do it this way, otherwise the processing in onMessage () will actually be valid.

Another suggestion I can give is that you should take a look at CDI , which is a new addition to the EE 6 specification and provides rich dependency injection.



Whether you are using MDB to perform asynchronous operations, Servlet 3.0 has some neat asynchronous capabilities . I suggest you watch the entire presentation if you are not familiar with Servlet 3.0.

+6


a source







All Articles