kis*_*tal 2 servlets jms message-driven-bean java-ee-6 ejb-3.1
使用ejb 3.1,servlet 3.0(glassfish服务器v3)
场景:我有MDB,它监听jms消息并处理其他会话bean(无状态).Servelet注入jms资源.
问题1:为什么servlet在使用静态声明时不能注入jms资源?
@Resource(mappedName = "jms/Tarturus")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/StyxMDB")
private static Queue queue;
private Connection connection;
Run Code Online (Sandbox Code Playgroud)
和
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
[#| 2010-05-03T15:18:17.118 + 0300 |警告| glassfish3.0 | javax.enterprise.system.container.web.com.sun.enterprise.web | _ThreadID = 35; _ThreadName =线程1; | StandardWrapperValve [WorkerServlet]:PWC1382:为servlet WorkerServlet分配异常com.sun.enterprise.container.common.spi.util.InjectionException:在com.sun上为类ua.co.rufous.server.services.WorkerServiceImpl创建托管对象时出错.企业.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:312)位于com.sun.enterprise.web.WebModule的com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:709). createServletInstance(WebModule.java:1937)at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1252)引起:com.sun.enterprise.container.common.spi.util.InjectionException:异常尝试注入未解决的Message-Destination-Ref ua.co.rufous.server.services.WorkerServiceImpl/queue @ java.lang.String @ null into class ua.co.rufous.com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:614)中的server.services.WorkerServiceImpl,地址为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.createManagedObject(InjectionManagerImpl.java:306)... 27更多引起:com.sun.enterprise.container.common.spi. util.InjectionException:在com.sun.enterprise.container.common.impl上仅支持基于实例的注入的类上非法使用静态字段private static javax.jms.Queue ua.co.rufous.server.services.WorkerServiceImpl.queue .util.InjectionManagerImpl._inject(InjectionManagerImpl.java:532)... 31更多|#]
我的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) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,没有静态声明:
@Resource(mappedName = "jms/Tarturus")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/StyxMDB")
private Queue queue;
private Connection connection;
Run Code Online (Sandbox Code Playgroud)
问题2:使用MDB的最佳实践是什么:在onMessage()中处理完整请求或在onMessage()方法中调用另一个bean(在我的情况下为无状态bean),它将处理它.处理包括很少调用肥皂服务,因此整个处理时间可能是3秒.
谢谢.
| 归档时间: |
|
| 查看次数: |
5955 次 |
| 最近记录: |