Question

I'm using jms with jboss/wildfly (jee6).

A JMS Queue can easily be injected using:

@Resource(name="java:jboss/exported/jms/queue/queuename")
private Queue myQueue;

Now I'd like to implement a central Stateless Bean which gets all the queues injected and which provides a single String parameterized method to retrieve the Queue in a factory like way:

@Resource(name="java:jboss/exported/jms/queue/queuename1")
private Queue myQueue1;

@Resource(name="java:jboss/exported/jms/queue/queuename2")
private Queue myQueue2;

public Queue getQueueIWant(String identifier) {
  if("IdentifyingString".equals(identifier))
  { return myQueue1; }
  if...
}

Inside another Bean this "FactoryBean" gets injected:

@EJB
private MyQueueFactory queueFactory;

and can easily be used:

...
Queue queue = queueFactory.getQueueIWant("AIdentifier");
producer = session.createProducer(queue);
...

The retrieved Queue instance will be used to send messages to the queue (MessageProducer) and to retrieve them (MessageConsumer) in different places (Beans). I have tried this implementation and it seems to work.

My Question is does anyone see any Problems with this? Could this lead to instability? Should i use jndiLookup instead? Is there a better/easier way? Or is this nice and possible :-) ?

Thanks Philipp

Was it helpful?

Solution

In your code you have managed to centralize the resource declration/injection, but at your usage site you still need to address each queue individually. So not much has been gained compared with direct injection.


It seems that you are trying to have something like an array of queues together with resource injection, but resource injection in that case does not really scale: Each new queue requires a deployment.

In your case I suggest using a manual JNDI lookup in an iteration. Then you can put the queues in an array/list for further processing. You could even pass the number of queues as a parameter into the method, and so the number of queues can be changed dynamically at run-time:

Pseudo-code (not tested, just for illustration):

InitialContext ic = new InitialContext();
Queue[] qs = new Queue[count];
for (int i = 0; i < count; i++) {
  String name = "queue/queuename" + i;
  qs[i] = (Queue) ic.lookup(name);
}

If the queue name is based on a run-time argument (like an incoming JMS message) a JNDI lookup would be suitable, because static resource injection and dynamic naming conflicts somehow. Using a dynamic JNDI lookup scales, as a deployment to support additional queues is not required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top