Skip to main content

howto: How can the same bean multiple times configured differently ?

11 replies [Last post]
frenchdrip
Offline
Joined: 2007-11-05
Points: 0

How can the same bean multiple times configured differently (i.e. listening to different queues) ?

I've tried it but break my bean's @notation resource references. I think I want a configuration sort of like this:

ejb-jar.xml:

FabToHubSenderSeattleSBean
hello.FabToHubSenderLocal
hello.FabToHubSenderRemote
hello.FabToHubSenderSBean
Stateless
Container

FabToHubSenderHongKongSBean
hello.FabToHubSenderLocal
hello.FabToHubSenderRemote
hello.FabToHubSenderSBean
Stateless
Container

sun-ejb-jar.xml:

FabToHubSenderSeattleSBean

jms/FabZzzNnFactory
jms/FabSeattleFactory

jms/FabZzzNnToHubQueue
jms/FabSeattleToHubQueue

FabToHubSenderHongKongSBean

jms/FabZzzNnFactory
jms/FabHongKongFactory

jms/FabZzzNnToHubQueue
jms/FabHongKongToHubQueue

class:

@Stateless
@Local({FabToHubSenderLocal.class})
@Remote({FabToHubSenderRemote.class})
//@EJB(name = "XXX")
public class FabToHubSenderSBean implements FabToHubSenderLocal, FabToHubSenderRemote
{
@Resource(name = "jms/FabZzzNnFactory")
private ConnectionFactory connectionFactory;

@Resource(name = "jms/FabZzzNnToHubQueue")
private Queue queue;

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
frenchdrip
Offline
Joined: 2007-11-05
Points: 0

Thanks for all your help.

sdoyle
Offline
Joined: 2007-10-11
Points: 0

It would be good to see JEE6 specifics. I'm very much interested in the WebBeans part of the JSR.

S.D.

sdoyle
Offline
Joined: 2007-10-11
Points: 0

It would be good to see JEE6 specifics. I'm very much interested in the WebBeans part of the JSR.

S.D.

whartung
Offline
Joined: 2003-06-13
Points: 0

You want the same MDB implementation to listen to multiple queues?

I don't think that's really possible (not in the same application at least, perhaps installed in several different EARs...).

There's really only "one" MDB in your application. There may be several instances, each processing their own message, but they're all listening to the same queue/topic. But the construct as a whole is a single entity.

You pretty much need several MDBs, one for each queue/topic. If they share code, you can refactor it out in to a common set of classes, or perhaps a Session Bean.

frenchdrip
Offline
Joined: 2007-11-05
Points: 0

[b]I'm pretty sure it is possible to configure the same MDB to listen to different queues sort of like this:[/b]


FabToHubSenderHongKongSBean

jms/FabZzzNnFactory
jms/FabHongKongFactory


jms/FabZzzNnToHubQueue
jms/FabHongKongToHubQueue

[b]The trouble is when I set the MDB names to something other than the ejb-name, the references in the class files can't be found.[/b]

cf126330
Offline
Joined: 2005-03-29
Points: 0

It's certainly possible to deploy n EJBs that have the same ejb-class. It's one of those things that deployment descriptors can do but annotations can't.

But be aware of this issue:
https://glassfish.dev.java.net/issues/show_bug.cgi?id=3547
Environment resource injection fails in MDB.

Actually this issue affects MDB and session beans as well. To work around it, lookup your resources in lieu of injection.

Use for ConnectionFactory, DataSource, etc.
Use for jms dest

-cheng

frenchdrip
Offline
Joined: 2007-11-05
Points: 0

[b]Thanks, I'll try doing the context lookup.

However, I'm not sure if I've got my ejb-jar.xml alright. Does what I have look reasonable? Do I need something like a mapped name or ref-name element?
I'm getting this error:[/b]

deployed with moduleid = dam-ejb-fab
Binding name:`java:comp/env/jms/FabZzzNnToPtsQueue`
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
**RemoteBusinessJndiName: com.ilmn.dam.fab.jms.FabToPtsSenderRemote; remoteBusIntf: com.ilmn.dam.fab.jms.FabToPtsSenderRemote
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
**RemoteBusinessJndiName: com.ilmn.dam.fab.jms.FabToHubSenderRemote; remoteBusIntf: com.ilmn.dam.fab.jms.FabToHubSenderRemote
Binding name:`java:comp/env/jms/FabZzzNnToHubQueue`
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
POARemoteRefFactory checking if SFSBVersionPolicy need to be added
EJBSCLookup:: sc.getEjbContainerAvailabilityEnabledFromConfig() ==> false
POARemoteRefFactory addSFSBVersionPolicy? false
EJB5090: Exception in creating EJB container [javax.naming.NameAlreadyBoundException: Use rebind to override]
appId=dam-ejb-fab moduleName=C__NetBeansProjects_dam-prototype_dam-ejb-fab_build_jar ejbName=FabToHubSenderSBean
LDR5012: Jndi name conflict found in [dam-ejb-fab]. Jndi name [com.ilmn.dam.fab.jms.FabToHubSenderRemote] for bean [FabToHubSenderSBean] is already in use.
LDR5013: Naming exception while creating EJB container:
javax.naming.NameAlreadyBoundException: Use rebind to override

cf126330
Offline
Joined: 2005-03-29
Points: 0

1. NameAlreadyBoundException may have been caused by the fact that all your EJBs use the same default global jndi-name, since they have the same remote business interface. I suggest you use a sun-ejb-jar.xml to declare their global jndi-name:






FabToHubSenderSeattleSBean
FabToHubSenderSeattleSBean

jms/FabZzzNnFactory
jms/FabSeattleFactory


jms/FabZzzNnToHubQueue
jms/FabSeattleToHubQueue



FabToHubSenderHongKongSBean
FabToHubSenderHongKongSBean

jms/FabZzzNnFactory
jms/FabHongKongFactory


jms/FabZzzNnToHubQueue
jms/FabHongKongToHubQueue



2. remove all annotations from your bean class, since you already use descriptors anyway. Why not keep the metadata in one place? Another reason is, with @Stateless annotation on bean class, you are declaring one extra EJB that you don't use.

3. In your java code, look up like this:
InitialContext ic = new InitialContext();
Queue queue = (Queue) ic.lookup("java:comp/env/jms/FabZzzNnToHubQueue");

4. Your ejb-jar.xml looks right. Make sure you add these for EACH EJB.

5. It's also possible to use element in ejb-jar.xml to map EJBs and resources to their global jndi-name, thus avoiding using sun-ejb-jar.xml. But I'd rather avoid it to keep the app portable. mapped-name or mappedName is not portable in JavaEE 5.

-cheng

Message was edited by: cf126330

frenchdrip
Offline
Joined: 2007-11-05
Points: 0

Thanks, setting the jndi-name in the sun-ejb-jar.xml resolves the name conflict for the session beans. Perfect!!!

For message beans however, Dies Koper said to use jndi-name in the sun-ejb-jar.xml to specify which queue a MDB listens to. Was this incorrect?

http://forums.java.net/jive/message.jspa?messageID=243964

[i]
HubToFabReceiverMDBean
YourQueue
[/i]

Dies Koper

In case of MDBs with a JMS listener, that jndi-name tag is used to
specify the queue or topic name in your environment.
For the other EJBs it is used to denote the name to use to bind the
bean's remote (business) interface in the JNDI directory.

Note that MDBs do not have a business interface: clients will not try to
look it up, so MDBs do not need their name bound in the JNDI directory.

So depending on the bean type this tag is used for different purposes.

glassfish@javadesktop.org wrote:
> Thanks, setting the jndi-name in the sun-ejb-jar.xml resolves the name conflict for the session beans. Perfect!!!
>
> For message beans however, Dies Koper said to use jndi-name in the sun-ejb-jar.xml to specify which queue a MDB listens to. Was this incorrect?
>
> http://forums.java.net/jive/message.jspa?messageID=243964
>
> [i]
> HubToFabReceiverMDBean
> YourQueue
> [/i]
> [Message sent by forum member 'frenchdrip' (frenchdrip)]

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

frenchdrip
Offline
Joined: 2007-11-05
Points: 0

[i]> So depending on the bean type this tag is used for different purposes.[/i]

What sneaky contextual reuse of the jndi-name element. That makes sense now though. Thank you.