Below is the code of Singleton Startup EJB (InstallServicesBean), it uses
another EJB (MyServiceBean) to access database. MyServiceBean is located in
the same bundle.
@Startup @Singleton @DependsOn("adminServiceBean") public class
InstallServicesBean { private static Logger logger =
LoggerFactory.getLogger(InstallServicesBean.class); @Inject
private MyServiceListener listener; @Inject private
MyServiceBean adminService; @PostConstruct public void
installService() { BundleContext m_context =
ServiceActivator.m_context; String filter = "(" +
Constants.OBJECTCLASS + "=" + BaseService.class.getName() + ")";
ServiceReference[] refs = null; //add a service listener
try {
m_context.addServiceListener(listener, filter);
logger.debug("Added a service listener to install services");
refs = m_context.getServiceReferences(null, filter);
} catch (InvalidSyntaxException e) {
logger.error("InvalidSyntaxException: ", e); }
if (refs != null) { for (ServiceReference r : refs) {
//send pseudo events for existing services
listener.serviceChanged(new
ServiceEvent(ServiceEvent.REGISTERED, r)); }
} } private class MyServiceListener implements
ServiceListener { public synchronized void
serviceChanged(ServiceEvent event) { switch
(event.getType()) { case
ServiceEvent.REGISTERED: //try
{//lookup EJB every time, otherwise we get exception "Attempt to invoke
when container is in Undeployed"
// adminService = (MyServiceBean) new
InitialContext().lookup("java:global/com.ex.test-war/adminServiceBean");
//} catch (NamingException e) {
//
logger.error("MyServiceListener - exception: ",e);
//}
//do something
adminService.installService(serviceName);
break; }
} } }
thank you
--
[Message sent by forum member 'okna2000']
View Post: http://forums.java.net/node/808455