8 Replies Latest reply: Oct 11, 2012 10:44 AM by gtriant RSS

    Get MDB name from within the MDB

    gtriant
      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
        • 1. Re: Get MDB name from within the MDB
          965123
          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.
          • 2. Re: Get MDB name from within the MDB
            gimbal2
            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?
            • 3. Re: Get MDB name from within the MDB
              gtriant
              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.
              • 4. Re: Get MDB name from within the MDB
                gimbal2
                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.
                • 5. Re: Get MDB name from within the MDB
                  gtriant
                  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
                  • 6. Re: Get MDB name from within the MDB
                    jtahlborn
                    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.
                    • 7. Re: Get MDB name from within the MDB
                      gtriant
                      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
                      • 8. Re: Get MDB name from within the MDB
                        gtriant
                        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.