目前我正在创建企业应用程序客户端,它应该触发会话 bean 向主题发送消息。这段代码是根据 j2ee-6 文档JMS application with session bean创建的。不幸的是,它不起作用,我找不到错误。1. 源代码在 EA 客户端项目中, 2. 在 Java 类库项目中, 3,4. 在 EJB 模块项目中。1. 项目在库中对 2. 和 3. 有依赖关系。
企业应用客户端:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.polgrabiat.jmsbean;
import javax.ejb.EJB;
/**
*
* @author Tomasz Półgrabia <tpsa.s.a@gmail.com>
*/
public class JMSClient {
/**
* @param args the command line arguments
*/
@EJB
static private PublisherBeanRemote publisherBeanRemote;
public static void main(String[] args) {
publisherBeanRemote.publishNews();
publisherBeanRemote.publishNews();
}
}
远程接口:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.polgrabiat.jmsbean;
import javax.ejb.Remote;
/**
*
* @author Tomasz Półgrabia <tpsa.s.a@gmail.com>
*/
@Remote
public interface PublisherBeanRemote {
void publishNews();
String chooseType();
}
PublisherBean
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.polgrabiat.jmsbean;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
/**
*
* @author Tomasz Półgrabia <tpsa.s.a@gmail.com>
*/
@Stateless
@Remote(PublisherBeanRemote.class)
public class PublisherBean implements PublisherBeanRemote {
@Resource
private SessionContext sc;
@Resource(lookup = "TopicConnectionFactory")
private TopicConnectionFactory topicConnectionFactory;
@Resource(lookup = "JMSTopic")
private Topic topic;
private TopicConnection topicConnection;
final static String messageTypes[] = {"Nation/World",
"Metro/Region", "Business", "Sports", "Living/Arts",
"Opinion"};
@Override
public void publishNews() {
TopicSession topicSession = null;
TopicPublisher topicPublisher;
TextMessage textMessage;
int numMsgs = messageTypes.length * 3;
String messageType;
try {
topicSession = topicConnection.createTopicSession(true, 0);
topicPublisher = topicSession.createPublisher(topic);
textMessage = topicSession.createTextMessage();
for (int i = 0; i < numMsgs; i++) {
messageType = chooseType();
textMessage.setStringProperty("NewsType", messageType);
textMessage.setText("Item " + i + ": " + messageType);
System.out.println("PUBLISHER: Setting "
+ "message text to: " + textMessage.getText());
topicPublisher.publish(textMessage);
}
} catch (JMSException ex) {
Logger.getLogger(PublisherBean.class.getName()).log(Level.SEVERE, null,
ex);
sc.setRollbackOnly();
} finally {
if (topicSession != null) {
try {
topicSession.close();
} catch (JMSException ex) {
Logger.getLogger(PublisherBean.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
}
@Override
public String chooseType() {
return messageTypes[new Random().nextInt(messageTypes.length)];
}
@PostConstruct
public void ejbCreate() {
try {
topicConnection = (TopicConnection) topicConnectionFactory.createConnection();
} catch (JMSException ex) {
Logger.getLogger(PublisherBean.class.getName()).log(Level.SEVERE, null,
ex);
}
}
@PreDestroy
public void ejbRemove() {
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException ex) {
Logger.getLogger(PublisherBean.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
}
消息豆:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pl.polgrabiat.jmsbean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
*
* @author Tomasz Półgrabia <tpsa.s.a@gmail.com>
*/
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "subscriptionDurability",
propertyValue = "durable"),
@ActivationConfigProperty(propertyName = "messageSelector",
propertyValue = "NewsType = 'Sports' OR NewsType = 'Opinion'"),
@ActivationConfigProperty(propertyName = "connectionFactoryLookup",
propertyValue = "TopicConnectionFactory"),
@ActivationConfigProperty(propertyName = "clientId",
propertyValue = "JMSTopic"),
@ActivationConfigProperty(propertyName = "subscriptionName",
propertyValue = "MySub"),
@ActivationConfigProperty(propertyName = "destinationLookup",
propertyValue = "JMSTopic"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Topic")
})
public class MessageBean implements MessageListener {
public MessageBean() {
}
@Override
public void onMessage(Message inMessage) {
TextMessage txtMsg;
if (inMessage instanceof TextMessage) {
try {
txtMsg = (TextMessage) inMessage;
System.out.println("Got message: " + txtMsg.getText());
} catch (JMSException ex) {
Logger.getLogger(MessageBean.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.err.println("Message in wrong type: "
+ inMessage.getClass().getName());
}
}
}
部署:
ant -f F:\\Documents\\projekty\\JMSClient2 -DforceRedeploy=false run
init:
JMSBean.init:
JMSClassLibrary.init:
Deleting: F:\Documents\projekty\JMSClassLibrary\build\built-jar.properties
JMSClassLibrary.deps-jar:
Updating property file: F:\Documents\projekty\JMSClassLibrary\build\built-jar.properties
JMSClassLibrary.compile:
JMSClassLibrary.jar:
JMSBean.deps-jar:
Compiling 1 source file to F:\Documents\projekty\JMSBean\build\classes
JMSBean.compile:
JMSBean.library-inclusion-in-archive:
Building jar: F:\Documents\projekty\JMSBean\dist\JMSBean.jar
JMSBean.dist:
JMSClassLibrary.init:
Deleting: F:\Documents\projekty\JMSClassLibrary\build\built-jar.properties
JMSClassLibrary.deps-jar:
Updating property file: F:\Documents\projekty\JMSClassLibrary\build\built-jar.properties
JMSClassLibrary.compile:
JMSClassLibrary.jar:
deps-jar:
compile:
library-inclusion-in-archive:
Building jar: F:\Documents\projekty\JMSClient2\dist\JMSClient2.jar
dist:
pre-run-deploy:
Redeploying F:\Documents\projekty\JMSClient2\dist\JMSClient2.jar
Initializing...
post-run-deploy:
run-deploy:
Copying 1 file to F:\Documents\projekty\JMSClient2\dist
Copying 2 files to F:\Documents\projekty\JMSClient2\dist\JMSClient2Client
Warning: F:\Documents\projekty\JMSClient2\dist\gfdeploy\JMSClient2 does not exist.
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=pl.polgrabiat.jmsbean.JMSClient/publisherBeanRemote,Remote 3.x interface =pl.polgrabiat.jmsbean.PublisherBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=pl.polgrabiat.jmsbean.PublisherBeanRemote,refType=Session into class pl.polgrabiat.jmsbean.JMSClient: Lookup failed for 'java:comp/env/pl.polgrabiat.jmsbean.JMSClient/publisherBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl}
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:717)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:484)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:212)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:204)
at org.glassfish.appclient.client.acc.AppClientContainer$ClientMainClassSetting.getClientMainClass(AppClientContainer.java:636)
at org.glassfish.appclient.client.acc.AppClientContainer.getMainMethod(AppClientContainer.java:525)
at org.glassfish.appclient.client.acc.AppClientContainer.completePreparation(AppClientContainer.java:419)
at org.glassfish.appclient.client.acc.AppClientContainer.prepare(AppClientContainer.java:320)
at org.glassfish.appclient.client.AppClientFacade.prepareACC(AppClientFacade.java:279)
at org.glassfish.appclient.client.acc.agent.AppClientContainerAgent.premain(AppClientContainerAgent.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:382)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:397)
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/pl.polgrabiat.jmsbean.JMSClient/publisherBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=pl.polgrabiat.jmsbean.JMSClient/publisherBeanRemote,Remote 3.x interface =pl.polgrabiat.jmsbean.PublisherBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=pl.polgrabiat.jmsbean.PublisherBeanRemote,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote not found]]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:613)
... 15 more
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=pl.polgrabiat.jmsbean.JMSClient/publisherBeanRemote,Remote 3.x interface =pl.polgrabiat.jmsbean.PublisherBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=pl.polgrabiat.jmsbean.PublisherBeanRemote,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote not found]]
at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:188)
at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1143)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:745)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:715)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:159)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:471)
... 18 more
Caused by: javax.naming.NamingException: Lookup failed for 'pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote not found]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:183)
... 23 more
Caused by: javax.naming.NameNotFoundException: pl.polgrabiat.jmsbean.PublisherBeanRemote#pl.polgrabiat.jmsbean.PublisherBeanRemote not found
at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66)
at com.sun.enterprise.naming.impl.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Java Result: 1
run:
BUILD SUCCESSFUL (total time: 38 seconds)