在调用Produces后加载启动bean

时间:2017-07-24 19:58:58

标签: java java-ee ejb-3.0

我的应用程序中有一个两个模块,其中第一个模块有一个单独的启动bean,我将本地服务绑定为JNDI资源。在第二个模块中,我有一个Producer,我想查找这个资源,但在整个生命周期中遇到问题,因为Produces在启动Bean之前启动调用,结果无法加载资源。这是我的代码:

@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();
    }

}

这是其他模块中的Producer类:

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);
        }
    }
}

有人可以告诉我为什么在WebSocketServiceLocator之后加载@startup bean吗?

1 个答案:

答案 0 :(得分:1)

根据您使用的标签,ejb-3.0尚未提供@Startup。 如果您使用3.1,也许您可​​以同时创建@Startup Singletons并使用@DependsOn来定义bean的序列。 see