Startup bean is loading after calling Produces

Viewed 343

I have a two module in my application in which first module have a singleton startup bean where i bind my local service as a JNDI resource. In second module i have a Producer where i want to lookup this resource but getting a problem in whole life cycle as Produces is start invoke before the startup Bean and result is failed to load 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 a Producer class in other 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 someone tell me why @startup bean loading after a WebSocketServiceLocator ?

1 Answers
Related