1 2 Previous Next 22 Replies Latest reply on Jul 13, 2009 8:00 AM by leosbitto

    Embedded server - how to create JMS Topic?

    leosbitto

      I am trying to embed JBoss Messaging 2.0 beta to my application which uses JMS API. I can easily create JMS Queue this way:

      SimpleString q = new SimpleString("jms.queue."+name);
      server.createQueue(q, q, null, true, false);

      where "server" is MessagingServer which i create this way:

      Configuration configuration = new ConfigurationImpl();
      configuration.setPersistenceEnabled(false);
      configuration.setSecurityEnabled(false);
      configuration.setAcceptorConfigurations(transports);
      server = Messaging.newMessagingServer(configuration);
      server.start();

      However, I do not know how to create JMS Topic. Any hint, please?

        • 1. Re: Embedded server - how to create JMS Topic?
          jmesnil

          The queue you created is a core queue.

          The preferred way to create JMS destinations is using the JMSServerManagerImpl on top of the MessagingServer you created:

          Configuration configuration = new ConfigurationImpl();
          configuration.setPersistenceEnabled(false);
          configuration.setSecurityEnabled(false);
          configuration.setAcceptorConfigurations(transports);
          server = Messaging.newMessagingServer(configuration);
          JMSServerManager manager =new JMSServerManagerImpl(server);
          
          manager.createQueue(...);
          manager.createTopic(...);
          
          manager.start(); // will also start the underlying MessagingServer
          


          • 2. Re: Embedded server - how to create JMS Topic?
            ataylor

            When using the core API there i s no such thing as a topic, we only deal with queues. Basically a topic consists of an address with multiple queues bound to it where each queue will have a consumer(subscriber).

            If you are using JMS then i would recommend taking a look at the JMSServerManager, you could do something like:

            [codeJMSServerManager ]jmsServer = new JMSServerManagerImpl(server);
            jmsServer.createTopic(.....

            • 3. Re: Embedded server - how to create JMS Topic?
              timfox

              To clarify Jeff's answer a bit:

              The MessagingServer you created is a "core" server. A core server knows nothing about JMS - it's completely JMS agnostic, therefore it knows nothing about topics.

              This is discussed in the user manual:

              http://www.jboss.org/file-access/default/members/jbossmessaging/freezone/docs/usermanual-2.0.0.beta3/html/architecture.html

              Now, the question I would ask is, if embedding do you really want JMS? Why not just use the core API? It's simpler and it can do everything that JMS does (and more)

              • 4. Re: Embedded server - how to create JMS Topic?
                leosbitto

                Thanks a lot for all the quick answers! Navigating me to JMSServerManagerImpl was exactly what I needed. I do not like JMSServerManagerImpl much for embedding, though, because of two reasons. First is that it somehow automagically picks the file jbm-jms.xml, which in my case belongs to a different server instance and therefore must not be used. Second is that it registers the queues and topics into JNDI (passing null as the secong argument to createTopic leads to NullPointerException) and I am trying to avoid JNDI in this case. So I have just borrowed a bit of your code from JMSServerManagerImpl.java and this works fine for me:

                final SimpleString t = JBossTopic.createAddressFromName(name);
                server.createQueue(t, t, new SimpleString("__JBMX=-1"), true, false);

                It would be nice if you could make the String "__JBMX=-1" available somewhere from your code. Maybe just change the private static final field REJECT_FILTER in JMSServerManagerImpl from private to public?

                Regarding using the core API directly instead of JMS - that is a possibility, of course. However, I am trying to stick with JMS, because that makes my code compatible with many other messaging implementations, too, instead of binding it tightly to JBoss Messaging.

                • 5. Re: Embedded server - how to create JMS Topic?
                  timfox

                  Hi Leos-

                  JMSServerManager is only really used for loading JMS queue, topic and connection factory descriptions from jbm-jms.xml into JNDI and also registering them with JMX. The JMX call to createTopic in JMSServerManager basically just does that. (Actually I think we should rename them to "deployTopic" or "bindTopic" or something similar).

                  If you're not using JNDI and don't need them in JMX then you shouldn't need to use it.

                  If you're using JMS you should just be able to instantiate the JMS queue and topic instances directly on the client side, e.g.

                  Topic myTopic = new JBossTopic("myTopic");

                  ... do stuff with the topic.

                  Can you tell me if that works?

                  • 6. Re: Embedded server - how to create JMS Topic?
                    jmesnil

                     

                    "Leos.Bitto" wrote:
                    Thanks a lot for all the quick answers! Navigating me to JMSServerManagerImpl was exactly what I needed. I do not like JMSServerManagerImpl much for embedding, though, because of two reasons. First is that it somehow automagically picks the file jbm-jms.xml, which in my case belongs to a different server instance and therefore must not be used. Second is that it registers the queues and topics into JNDI (passing null as the secong argument to createTopic leads to NullPointerException) and I am trying to avoid JNDI in this case.


                    Regarding jbm-jms.xml, it will be picked up if it is in the classpath. Could you place it in a separate directory and add it to the classpath of the other server instance instead?

                    For JNDI, you could pass a no-op Context implementation to JMServerManager.setContext() to "disable" JNDI.

                    Do not hesitate to fill JIRA issues for this kind of improvements.

                    Thanks for the feedback,
                    jeff


                    • 7. Re: Embedded server - how to create JMS Topic?
                      ataylor

                      also you can use

                      session.createTopic(topicName);
                      


                      • 8. Re: Embedded server - how to create JMS Topic?
                        leosbitto

                        Hi Tim,

                        "timfox" wrote:

                        JMSServerManager is only really used for loading JMS queue, topic and connection factory descriptions from jbm-jms.xml into JNDI and also registering them with JMX. The JMX call to createTopic in JMSServerManager basically just does that. (Actually I think we should rename them to "deployTopic" or "bindTopic" or something similar).


                        As soon as I was pointed to JMSServerManager I read the source code, suspected that I could live without JMSServerManager and it seems that it is really true.

                        My application uses the ApplicationContext from Spring Framework, so I already have an XML configuration file, and it is easy to add few lines there to create the necessarry JMS queues and topics, using my simple class which calls your core API. The same is true for creating the JMS (XA)ConnectionFactory - no JNDI needed.

                        "timfox" wrote:

                        If you're not using JNDI and don't need them in JMX then you shouldn't need to use it.


                        Yes, I do not use JNDI - I feel that for an embedded server JNDI is not necessary. I am not sure what do you mean by "in JMX" - the way I create the JMS queue and topic I can see them in JMX with all the cool stuff like message counts, etc.

                        "timfox" wrote:

                        If you're using JMS you should just be able to instantiate the JMS queue and topic instances directly on the client side, e.g.

                        Topic myTopic = new JBossTopic("myTopic");

                        ... do stuff with the topic.


                        Sure, that is documented nicely and I do that.

                        "timfox" wrote:

                        Can you tell me if that works?


                        No problem, I can send you my sample code if you let me know how - should I just paste in into this forum?

                        • 9. Re: Embedded server - how to create JMS Topic?
                          leosbitto

                           

                          "jmesnil" wrote:

                          Regarding jbm-jms.xml, it will be picked up if it is in the classpath. Could you place it in a separate directory and add it to the classpath of the other server instance instead?


                          Sure, I could do that. My setup was a bit messy, because I was just experimenting with the possibilities which JBoss Messaging provides. The real production deployment would be different for sure.

                          "jmesnil" wrote:

                          For JNDI, you could pass a no-op Context implementation to JMServerManager.setContext() to "disable" JNDI.


                          Thanks for the hint.

                          "jmesnil" wrote:

                          Do not hesitate to fill JIRA issues for this kind of improvements.


                          I will, as soon as I understand JBM a bit more. At this point I have only one small improvement to suggest: it the second argument of createQueue and createTopic of JMSServerManagerImpl would be null, simply do not register the object to JNDI instead of throwing a NullPointerException.

                          • 10. Re: Embedded server - how to create JMS Topic?
                            leosbitto

                             

                            "ataylor" wrote:
                            also you can use

                            session.createTopic(topicName);
                            


                            Nice, but what is "session"?

                            • 11. Re: Embedded server - how to create JMS Topic?
                              timfox

                               

                              "Leos.Bitto" wrote:

                              Nice, but what is "session"?


                              http://java.sun.com/javaee/5/docs/api/javax/jms/Session.html

                              • 12. Re: Embedded server - how to create JMS Topic?
                                leosbitto

                                 

                                "timfox" wrote:
                                "Leos.Bitto" wrote:

                                Nice, but what is "session"?


                                http://java.sun.com/javaee/5/docs/api/javax/jms/Session.html


                                We are misunderstood in this case. I need to create the topic, which this method does not accomplish - let me quote the specification:

                                Note that this method is not for creating the physical topic. The physical creation of topics is an administrative task and is not to be initiated by the JMS API. The one exception is the creation of temporary topics, which is accomplished with the createTemporaryTopic method.

                                • 13. Re: Embedded server - how to create JMS Topic?
                                  timfox

                                  OK fine, you should just be able to instantiate it directly as mentioned previously :)

                                  • 14. Re: Embedded server - how to create JMS Topic?
                                    ataylor

                                     

                                    We are misunderstood in this case. I need to create the topic, which this method does not accomplish - let me quote the specification:


                                    Yeah i know the spec :), i was assuming you had previously created the destination, this was so you could look it up with out using vendor specific code and no JNDI

                                    1 2 Previous Next