9 Replies Latest reply on Nov 26, 2004 10:32 AM by infectedrhythms

    Should I implement a service for...?


      Just wondering I will be implementing a few of my application as JBoss services...

      Because of some legacy code some of these service will require a regular TCP/IP server socket.

      Shoudl I create this "server" functionality as a service?

      If so how would I access this service from my other services? Thanks

        • 1. Re: Should I implement a service for...?
          Dimitris Andreadis Master

          Sure, that's a typical use. Implement you server socket-listener as you would anyway and wrap it with a nice MBean, controlling it's lifecycle with the start/stop methods. You'll probably create it's own listening thread/threads (or you may have a look at an already provided thread pool, jboss.system=service=ThreadPool).

          Your "other" services can access this MBean through it's MBean interface, so you may want to expose some additional operations, while the remote legacy clients will use your custom socket interface.

          • 2. Re: Should I implement a service for...?

            Just to be sure one of the config parameters for this mbean will be the server port right? This way each other service will have it's own server socket. So I wont have to write some complexe code to route to the right service...

            As of using the mbean interface, how is it done in code? Am still figuring out the whole java way of coding.


            • 3. Re: Should I implement a service for...?
              Dimitris Andreadis Master

              Yes, usually you need configuration attributes for the port and the binding address (to allow for hosts with more than one ip addresses).

              Take for example org.jboss.jmx.adaptor.snmp.trapd.TrapdService that creates an SNMP trap listener.

              In general, if your MBean wraps a "server" of a protocol/service, you'll only expose as MBean attributes the configuration parameters, necessary to configure the wrapped service, then hook into the lifecycle methods(create/start/stop/destroy) the logic for setting-up and shutting down your protocol/service implementation.

              In most cases, you won't need additional operations on your MBean, unless you want to provide management operations/attributes available through the jmx-console (e.g. listAllConnectedClients(), or RequestCounter).

              It quite simple once you get the basic idea :)

              • 4. Re: Should I implement a service for...?

                Cool thanks!

                So how do I access another service from a service. I nead a simple code example. If you can point me to one possibly...


                • 5. Re: Should I implement a service for...?
                  Dimitris Andreadis Master

                  Follow the example:

                  It's the same when going from MBean -> MBean, in the same JVM. This example is a bit advanced because it uses a dynamic proxy (i.e. a typed interface).

                  The low-level way is to get a reference to the MBean server (you have it already if you extend ServiceMBeanSupport, i.e. getServer()). Then you just use the MBeanServer interface to call the de-typed methods:


                  passing always the target MBean ObjectNam as parameter. Read the MBeanServer javadoc, and have a look at the JMX spec, too, and the 2nd chapter of the jboss documentation.

                  • 6. Re: Should I implement a service for...?

                    Oh lala some reading to do thanks!

                    • 7. Re: Should I implement a service for...?

                      I see how this works! So i figure I just have to import the interface of the Mbean I want to use in my service right?

                      • 8. Re: Should I implement a service for...?
                        Dimitris Andreadis Master

                        so, to recap, you need:

                        - a reference to the MBeanServer
                        - the ObjectName to the target MBean
                        - knowledge of the target MBean's exported interface to call operations (invoke()) or get/set attributes (getAttribute()/setAttribute()) using the un-typed (i.e. generic) MBeanServer API. In this case no import is needed.
                        - or, import the target MBean interface and use the dynamic proxy approach to use it as you would any typed object. The dynamic proxy internally uses the untyped method, anyway.

                        • 9. 3858490

                          Would you mind if I picked your brain on how to create this TCP/IP ServerSocker service?

                          So far am thinking when another service instantiates it... That it create a new instance of ServerSocket and binds it to a specific port.

                          How would I take care of the listening and receiving data from the ServerSocket service to the actuall service thats using it? I mean I know how to use ServerSocket infact that all I do at work more or less. But how would I "proxy" the listening and sending an receiving of data bewteen my service and the ServerSocket service...