我试图在本地计算机上的JBoss Wildfly上建立一个EJB容器,并且在一些帮助下,我设法使该容器启动并运行,并且识别了远程EJB Bean。但是,我希望它们使用不同的名称。
我在所有接口中都有一个NAME属性,其中包含实现Bean的所需名称。我将该字符串放在@Stateless(name = Interface.NAME)
中。
但是,仍然使用实现类名来创建bean。 我该怎么办?
编辑:我的EJB实现:
@Stateful(mappedName = StateService.NAME)
public class StateServiceImpl implements StateService {
private final Map<SessionId, Deque<State>> sessions;
private final List<StateChangeListener> listeners;
public StateServiceImpl() {
this.sessions = new HashMap<>();
this.listeners = new ArrayList<>();
}
@Override
public State getCurrentState(SessionId sessionId) {
return sessions.get(sessionId).peek();
}
@Override
public State popCurrentState(SessionId sessionId) {
State previousState = sessions.get(sessionId).pop();
State currentState = sessions.get(sessionId).peek();
StateChangeEvent event = new StateChangeEvent(previousState, currentState);
fireStateChangeEvent(event);
return currentState;
}
@Override
public Deque<State> getStateHistory(SessionId sessionId) {
return new LinkedList<>(sessions.get(sessionId));
}
@Override
public void updateState(SessionId sessionId, State state) {
State previousState = sessions.get(sessionId).peek();
if (!sessions.containsKey(sessionId)) {
sessions.put(sessionId, new LinkedList<>());
}
sessions.get(sessionId).push(state);
StateChangeEvent event = new StateChangeEvent(previousState, state);
fireStateChangeEvent(event);
}
@Override
public void addStateChangeListener(StateChangeListener listener) {
listeners.add(listener);
}
@Override
public void removeStateChangeListener(StateChangeListener listener) {
listeners.remove(listener);
}
protected void fireStateChangeEvent(StateChangeEvent event) {
listeners.forEach(listener -> listener.stateChanged(event));
}
}
我的界面:
/**
* Service interface to manage state changes on the server-side
*/
@Remote
public interface StateService {
String NAME = "StateService";
/**
* returns the most recent state stored for the given session id
*/
State getCurrentState(SessionId sessionId);
/**
* removes the last entry of session from the queue, returning the one before
*/
State popCurrentState(SessionId sessionId);
/**
* returns the whole history of a given client
*/
Deque<State> getStateHistory(SessionId sessionId);
/**
* adds a new session to the stack
*
* @param sessionId the client the state is updated for
* @param state the updated state
*/
void updateState(SessionId sessionId, State state);
void addStateChangeListener(StateChangeListener listener);
void removeStateChangeListener(StateChangeListener listener);
interface StateChangeListener {
void stateChanged(StateChangeEvent event);
}
@AllArgsConstructor
@Getter
class StateChangeEvent {
private final State previousState;
private final State nextState;
}
}
这些是绑定:
[INFO] [talledLocalContainer] java:global/web-be/web-impl-0.0.1-SNAPSHOT/MenuServiceImpl!io.ropi.fiveshelly.services.MenuService
[INFO] [talledLocalContainer] java:app/web-impl-0.0.1-SNAPSHOT/MenuServiceImpl!io.ropi.fiveshelly.services.MenuService
[INFO] [talledLocalContainer] java:module/MenuServiceImpl!io.ropi.fiveshelly.services.MenuService
[INFO] [talledLocalContainer] java:global/web-be/web-impl-0.0.1-SNAPSHOT/MenuServiceImpl
[INFO] [talledLocalContainer] java:app/web-impl-0.0.1-SNAPSHOT/MenuServiceImpl
[INFO] [talledLocalContainer] java:module/MenuServiceImpl
预先感谢
答案 0 :(得分:1)
我不知道您是否可以更改类本身的名称,但是作为示例,您可以使用已发布的JNDI的名称(取决于我项目的名称):
java:global/Depends/NameDesired!MyClassName
java:app/Depends/NameDesired!MyClassName
java:module/NameDesired!MyClassName
java:global/Depends/NameDesired
java:app/Depends/NameDesired
java:module/NameDesired
这个课是这样的:
import javax.ejb.Stateless;
@Stateless(name="NameDesired")
public class MyClassName {
public void doThing() {
System.out.println("Doing some work...");
}
}
然后您可以像这样使用
://I'm using here the fourth line of the JNDI, just using the name I want:
MyClassName example = (MyClassName) (new InitialContext().lookup("java:global/Depends/NameDesired"));
甚至
@EJB(beanName="NameDesired")
private MyClassName example;
请注意,您已经在使用正确的注释@Stateless(name =“”)。在聊天中,您可以使用字符串设置所需的名称。您甚至可以使用Environment Variable和如何使用Configure it进行查找,但就您而言,我不知道是否值得。