JEE 6 JMS示例

时间:2017-10-21 08:59:21

标签: java jms java-ee-6

我试图用JMS和glasfish 3做一个简单的消息发送者的虚拟示例。我创建了一个无状态bean,我在glasfish 3中定义了jsm / factory和jms / queue。代码bean是以下一个:

package converter.ejb.impl;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.*;
import java.util.Date;


@Stateless
public class SimpleMessageClientBean {

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

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

public void sampleMessageSend(){
    try {
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer = 
session.createProducer(queue);

        TextMessage jmsMessage = session.createTextMessage("Hi, this 
is a 'hello world' of JMS : "+new Date() );
        messageProducer.send(jmsMessage);
    } catch (JMSException e) {
        e.printStackTrace();
    }

}
}

我通过servlet调用它,调用EJB和方法sampleMessageSend。

每次我尝试调用此EJB消息时,都会收到以下异常:

javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
at com.sun.proxy.$Proxy197.sampleMessageSend(Unknown Source)
at converter.ejb.impl.__EJB31_Generated__SimpleMessageClientBean__Intf____Bean__.sampleMessageSend(Unknown Source)
at converter.web.MessageProducerServlet.doGet(MessageProducerServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
... 33 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
... 35 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Excepción al intentar inyectar Unresolved Message-Destination-Ref converter.ejb.impl.SimpleMessageClientBean/queue@java.lang.String@null en class converter.ejb.impl.SimpleMessageClientBean: Can not set javax.jms.Queue field converter.ejb.impl.SimpleMessageClientBean.queue to com.sun.messaging.jms.ra.DirectConnectionFactory
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1694)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:494)
... 37 more
Caused by: java.lang.IllegalArgumentException: Can not set javax.jms.Queue field converter.ejb.impl.SimpleMessageClientBean.queue to com.sun.messaging.jms.ra.DirectConnectionFactory
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:741)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:651)
... 41 more

我不明白问题的根源是什么。

1 个答案:

答案 0 :(得分:0)

抱歉。我的错误。我将glassfish中的队列定义为工厂。我有两个连接工厂,没有队列。我已经定义了一个资源命运和一个连接工厂,一切都很好。我的错 。

另一方面,我不得不将队列从'jms / queue'重命名为'testQueue'。似乎'jms / queue'不是一个好名字。我想这是一个字符'/'的问题。是否有关于JMS中队列名称和连接工厂的语法规则?