I have to implement a instance factory, that return a new message handler instance depend on message code.
There is a interface MessageHandler and about 50 implementations of this interface. Because of project structure (maven cyclic dependencies...) the factory class has no dependencies to concrete interface implementations.
A messageHandler interface:
interface MessageHandler {
void receive(Message message);
MessageCode getMessageCode();
}
Every MessageHandler implementation returns a unique message code. For example:
class FirstMessageHandler implements MessageHandler {
...
@Override
MessageCode getMessageCode(){
return MessageCodes.MESSAGE_TYPE_ONE;
}
...
}
class SecondMessageHandler implements MessageHandler {
...
@Override
MessageCode getMessageCode(){
return MessageCodes.MESSAGE_TYPE_TWO;
}
...
}
The MessageHandlerFactory should create new handler instance for every message. The factory has no information about concrete implementations and has no dependencies to the maven packages of the implementations. It has only the messageCodes.
class MessagehandlerFactory {
MessageHandler createHandler(MessageCode messageCode){
...
}
}
After searching i found a examples about @Produce annotation and this nice example:
http://docs.oracle.com/javaee/6/tutorial/doc/gkgkv.html
But in this example, i need to know all implementation types in the factory and this is not allowed.
Actually solution is to use @Any @Instance.
@Inject
@Any
private Instance<MessageHandler> messageHandlers;
@PostConstruct
public void init() {
for (MessageHandler handler : messageHandlers) {
messageHandlerMap.put(handler.getMessageCodes(), handler);
}
}
but this code create for every message instances of all MessageHandler implementations for every received message. It is possible to combine @Produce and @Qualifier so that only one MessageHandler instance will be created depend on received MessageCode?
I am using Glassfish 3.1.2.2.