The launch bean is loaded after calling Produces

I have two modules in my application where the first module has a singleton startup bean where I bind my local service as a JNDI resource. In the second module, I have a Producer where I want to look for this resource, but I get a problem throughout its lifecycle because Produces is started at startup before the bean is started and the result is not loaded by the resource. Here is my code:

@Slf4j
@Singleton
@Startup
public class WebSocketServerActivator {
    private static final Logger LOG = LoggerFactory.getLogger(WebSocketServerActivator.class);

    private void startServer() {
        try {
            WebSocketServer webSocketServer = WebSocketServer.getInstance();
            webSocketServer.setHost("localhost");
            webSocketServer.setPort(9900);
            webSocketServer.initialize();

            InitialContext ic = new InitialContext();
            ic.rebind(WebSocketSQLService.WEBSOCKET_SQL_JNDI, SQLRequestManager.getInstance());

        } catch (Exception e) {
            LOG.error("Error while starting webSocket in start activator ", e);
        }
    }

    @PostConstruct
    public void postConstruct() {
        startServer();
    }

}

      

Here is the Producer class in another module:

public class WebSocketServiceLocator {

    @Produces
    public WebSocketSQLService getWebSocketService(
            @JndiStringResource(value = "visma.websocket.server", defaultValue = WebSocketSQLService.WEBSOCKET_SQL_JNDI)
                    Supplier<String> websocketServerName
    ) {
        return (WebSocketSQLService) lookupService(websocketServerName.get());
    }

    private Object lookupService(String lookupName) {
        try {
            InitialContext ic = new InitialContext();
            return ic.lookup(lookupName);
        } catch (NamingException e) {
            throw new RuntimeException("Cannot find " + lookupName, e);
        }
    }
}

      

Can anyone tell me why the @startup bean is loaded after the WebSocketServiceLocator?

+3


source to share


1 answer


As per the tag, you are using ejb-3.0, which @Startup does not provide yet. If you are using 3.1, perhaps you can do both @Startup Singletons and use @DependsOn to define the sequence of beans. cm.



0


source







All Articles