3
我的應用程序中有兩個模塊,其中第一個模塊具有一個單例啓動bean,我將本地服務綁定爲JNDI資源。在第二個模塊中,我有一個Producer,我想查找這個資源,但是在整個生命週期中遇到問題,因爲Produces在啓動Bean之前會被調用,並且結果無法加載資源。這裏是我的代碼:啓動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();
}
}
這裏是其它模塊製作類:
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);
}
}
}
誰能告訴我,爲什麼後@Startup豆裝入一個WebSocketServiceLocator?
您是否知道@DependsOn如何在兩個EJB位於不同模塊時應用? – user565
該模塊不應該是一個問題,應該是EJB名稱。 @Singleton(name =「Something」) – aschoerk
實際上,它創建了一個問題,我有一個單獨的EJB(TestSingleton),它在WebSocketServerActivator EJB之前激活。我嘗試在TestSingleton上使用@DependsOn(「WebSocketServerActivator」)並獲得以下錯誤: java.lang.RuntimeException:EJB TestSingleton的DependsOn依賴關係'WebSocketServerActivator'無效。他們都在單獨的模塊。 – user565