I tried to use OpenEJB
for my EJB
Timer
task Unit test
. I get an error saying that it can't find the JNDI
name.
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
IService bean;
try {
Context context = new InitialContext(props);
bean = (IService) context.lookup("java:global/My-App/SingleOccurrenceServiceBean");
assertNotNull(bean);
} catch (NamingException e) {
e.printStackTrace();
}
And below is the log out put.
Apache OpenEJB 3.1.4 build: 20101112-03:32
http://openejb.apache.org/
INFO - openejb.home = D:\workspace\task\My-App
INFO - openejb.base = D:\workspace\task\My-App
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Configuring enterprise application: classpath.ear
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - Deployed Application(path=classpath.ear)
javax.naming.NameNotFoundException: Name "global/My-App/SingleOccurrenceServiceBean" not found.
Here is the Maven dependency.
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
version>3.1.4</version>
What could be the error?
@Remote
public interface IService {
public void initializeTimer(Date firstDate, long timeout, String info) throws RemoteException;
void cancelTimer(String timerName) throws RemoteException;
/**
* doService call when the EJBTimer timeout. And it start the service intend to do.
* @param timer
*/
@Timeout
public void doService(Timer timer);
}
@Stateless
public class SingleOccurrenceServiceBean implements IService{
@Override
public void initializeTimer(Date firstDate, long timeout, String info) throws RemoteException {
try {
timerService.createTimer(firstDate, info);
} catch (Exception e) {
log.fatal("Exception create timer : "+ e.toString());
e.printStackTrace();
}
}
@Override
public void cancelTimer(String timerName) throws RemoteException {
// do nothing as this timer is a single-event-timer
}
@Override
@Timeout
public void doService(Timer timer) {
log.debug("Read data from DB");
}
}
I was able to fix this. I had to add ejb-jar.xml
file in to resources
> META-INF
. But the file contains nothing except
<ejb-jar/>
and got a help from this link
Special Thank to Camilo for helping me so far.
Seems to me that the JNDI name you're looking up doesn't exist, in OpenEJB the default JNDI name for an EJB should be:
{deploymentId}{interfaceType.annotationName}
and you don't need the java:
, check the docs , and this post .
UPDATE
Try
...bean = (IService) context.lookup("SingleOccurrenceServiceBeanRemote");...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.