Forum Stats

  • 3,757,792 Users
  • 2,251,266 Discussions
  • 7,869,916 Comments

Discussions

Get MDB name from within the MDB

gtriant
gtriant Member Posts: 20
edited Oct 11, 2012 11:44AM in Java Message Service (JMS)
Hi,

Why would you want that, you may ask...
I have two MDBs that originate from the same source file. Either I keep and maintain twice the same code, or for the only difference that is required between them I do something to determine which one is executed currently by a message.

So, how do I retrieve statically the MDB name from within the MDB with java code?

Thanks

Answers

  • 965123
    965123 Member Posts: 16
    May be you only maintain one MDB but include something in the message that is used to determine what should happen.

    Another approach would be to use some kind of a configuration parameter maintained outside the MDB class.

    I haven't tried, but if you depend on annotations (for example @MessageDriven), then you should be able to easily use the reflections API to read the annotations and determine the name.

    Using the name of the MDB to determine how it should work doesn't sound right.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    gtriant wrote:
    I have two MDBs that originate from the same source file.
    I don't understand what that means. You mean you DEPLOY the same MDB twice?
  • gtriant
    gtriant Member Posts: 20
    I will be more specific but I dont think it is within the scope of the question to analyze the architecture and the reason behind this question.

    Here are parts of the deployment descriptors of two MDBs (EJB 2.0)

    1)
    <enterprise-beans>
    <message-driven>
    <ejb-name>MyQueueEjb</ejb-name>
    <ejb-class>mdb.MyQueueBean</ejb-class>
    <transaction-type>Container</transaction-type>
    <message-driven-destination>
    <destination-type>javax.jms.Queue</destination-type>
    </message-driven-destination>
    </message-driven>
    </enterprise-beans>

    2)
    <enterprise-beans>
    <message-driven>
    <ejb-name>MyRedeliveryQueueEjb</ejb-name>
    <ejb-class>mdb.MyQueueBean</ejb-class>
    <transaction-type>Container</transaction-type>
    <message-driven-destination>
    <destination-type>javax.jms.Queue</destination-type>
    </message-driven-destination>
    </message-driven>
    </enterprise-beans>


    These are two different MDBs "looking" to the same java class.
    So I am actually asking if from within this java class: MyQueueBean I can access the information of the <ejb-name> on runtime.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    You should get your terminology right; you're deploying two MDBs with the same class. Also it would have helped if you mentioned that this is legacy EJB 2.1 stuff.

    Apparently you want to do different things depending on the deployment name, so they are two different MDBs that might share a common base class with overlapping functionality. In other words: split into two classes.
  • gtriant
    gtriant Member Posts: 20
    I appreciate the suggestion. But as I said lets just keep this as a question of how to access the MDB name from within the MDB at runtime.
    Thanks
  • jtahlborn
    jtahlborn Member Posts: 2,040
    gtriant wrote:
    I appreciate the suggestion. But as I said lets just keep this as a question of how to access the MDB name from within the MDB at runtime.
    Thanks
    Insisting on a bad solution to a problem isn't going to help you. The best solution is to make 2 subclasses with the shared functionality in a common superclass. in fact, i seem to remember some app servers getting confused if you tried to deploy the same class multiple times under different names (at least for stateless ejbs). This is a simple, clean solution to your problem.
  • gtriant
    gtriant Member Posts: 20
    edited Oct 10, 2012 11:34AM
    The code/domain/architecture are existing and the good things you are describing are not an option.
    One MDB is the redelivery of the other (again... please dont ask why!) and I need to change the log filename. So there it is!

    So... as I very kindly requested earlier, if someone knows the answer to my actual question, I would really appreciate your help.

    Thanks again.

    Edited by: gtriant on Oct 10, 2012 8:33 AM
  • gtriant
    gtriant Member Posts: 20
    Just posting here a good solution to this question:

    http://www.coderanch.com/t/594787/EJB-JEE/java/EJB-name-within-EJB

    In short: Create a new JNDI entry with the same name in the deployment descriptors of the two MDBs, and parse the value during runtime through lookup.
This discussion has been closed.