13 Replies Latest reply on Jun 1, 2004 1:06 PM by brolly

    Running Java application inside JBoss

    brolly

      Hi.
      I currently deploy J2EE applications using ORION as my application Server. I've been trying to test JBoss to see how it compares to ORION to future developments. I've successfully configured JBoss to use most of what I need in my current applications (EJB,Web Services,JMS,etc).
      Still there is one thing I couldn't manage to do so far. In some cases I need to run some applications inside my Application server, mainly it will be a thread that will be monitoring either some database data, files or listening on a port for some input from other system.

      In Orion it was easy to do it. It would just be a matter of defining that in META-INF/Application.xml file of my EAR file and then simply setting auto-start in one of the orion configuration files.

      Application.xml example
      ...

      Monitor-Client.jar

      ...

      In JBoss when I do this I will get an error during startup:

      18:21:50,515 ERROR [MainDeployer] could not create deployment: file:/F:/JBoss/server/default/tmp/deploy/tmp23188eurApp2.ear-contents/Monitor-client.jar
      org.jboss.deployment.DeploymentException: Network is unreachable: connect; - nested throwable: (java.net.SocketException: Network is unreachable: connect) at org.jboss.metadata.XmlFileLoader.getDocument(XmlFileLoader.java:316) at org.jboss.metadata.XmlFileLoader.getDocument(XmlFileLoader.java:247)
      at org.jboss.deployment.ClientDeployer.create(ClientDeployer.java:93)
      at org.jboss.deployment.MainDeployer.create(MainDeployer.java:786)
      at org.jboss.deployment.MainDeployer.create(MainDeployer.java:778)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:641)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:605)
      at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)

      How can I make this application to run inside JBoss. I've seen that I can run services inside JBoss using MBeans. Should this be the way?
      I don't think that would be a good solution in this case since that would require addicional and specific coding into my application and I intend to be able to run it inside any J2EE app server and also be able to run it standalone if someday I intend to.

      Could anyone please help me out?

      Thanx

        • 1. Re: Running Java application inside JBoss
          brolly

          Part of my post was auto formatted I guess...

          Application.xml example should be:

          ...
          (module)
          (java)Monitor-Client.jar (/java)
          (/module)
          ...

          Replace '(' and ')' by '<' and '>'

          • 2. Re: Running Java application inside JBoss
            starksm64

            You are already using an app server proprietary feature that will not port. There is nothing in the specs that defines how to run a j2ee application client jar as part of an ear using 'auto-start'. Your only option in jboss is to use an MBean service.

            • 3. Re: Running Java application inside JBoss
              brolly

              Yes, I understand that. I was just trying to confirm if that would be the only way of doing it or if I was missing something.

              Personally I think a kind of configuration as ORION Server's is better since you don't need to change any code on your application, but I understand that the way JBoss was coded probably needs them to be MBeans.

              Still, there are a couple of things. First the applications probably won't be j2ee client apps, just simple standalone java apps (at least most of the times).

              Second, by using MBeans I'll have to add additional code to my application, so they won't run in standalone if I need to at some point.
              I have yet much to read about MBeans and I'll do it shortly. I guess I should be able to find a solution that will allow my app to run both in standalone and as MBeans. Do you have any advice or best practices to do it?

              Also, when using MBeans besides jboss-application.xml is there any other specific files I should include in the META-INF directory of my app jar file? And is there any way of telling JBoss that a specific MBean inside a EAR is to auto-start or not, or will JBoss start every single MBean declared in the EAR file?

              Thanks for the reply

              • 4. Re: Running Java application inside JBoss
                genman


                You're on the right track. I first suggest downloading the sourcecode and stealing an MBean example. The Wiki is a little thin on this, but the pay documentation has something. I did find this online:

                http://www.huihoo.com/jboss/online_manual/3.0/ch13s26.html

                The example here is a little bit complicated. (For example, you don't need to bind anything to JNDI, you can use MBeanServer to access you MBean.)

                • 5. Re: Running Java application inside JBoss

                  What's wrong with this example?

                  http://www.jboss.org/wiki/Wiki.jsp?page=ExampleHelloWorldService

                  To run an app as an mbean in jboss you just need:

                  
                  Thread thread;
                  
                  protected void startServce() throws Exception
                  {
                   thread = new Thread(new Runnable()
                   {
                   public void run()
                   {
                   try
                   {
                   MyApp.main(new String[0]); // replace with command line args
                   }
                   catch (InterruptedException e)
                   {
                   log.debug("Application interrupted", e);
                   }
                   catch (Throwable t)
                   {
                   log.warn("Unexpected error in application", t);
                   }
                   thread = null;
                   }
                   }
                   );
                   thread.setDaemon(true);
                   thread.start();
                  }
                  
                  protected void stopService() throws Exception
                  {
                   if (thread != null)
                   {
                   // Optionally interrupt the application
                   thread.interrupt();
                   // Optionally wait for it to finish
                   thread.join();
                   }
                  }
                  


                  • 6. Re: Running Java application inside JBoss
                    brolly

                    Thanx for the replies, they really helped.

                    It seems easy to implement that way.

                    So I won't even need a MANIFEST file in my app JAR to run the service right? All the info will be in the jboss-service.xml file is this correct?
                    If so my app will still be usuable as standalone.

                    Also please confirm if it is possible to have a service jar inside my EAR and configure JBoss to not start it.

                    I've now ran across XMBeans, for what I seen (just gave a really quick read) this can do the same as MBeans, but won't require any additional coding using XML to configure them. Would this be an alternate solution for my case?

                    • 7. Re: Running Java application inside JBoss

                      Add a META-INF/jboss-app.xml to your ear to define a sar or -service.xml module.
                      You can find plenty of discussion in the forums, including defining dependencies
                      if you need to access other services like ejbs.

                      You can also deploy an MBean by adding a -service.xml in the top level
                      of an ejb jar.

                      XMBeans are alternate to StandardMBeans that let you deploy any POJO rather than
                      than following the "MyService implements MyServiceMBean" pattern
                      You still need the deployment descriptor for XMBean

                      • 8. Re: Running Java application inside JBoss
                        hbaxmann

                        And you have the real advantage to seperate the Create, Start, Stop and Destroy steps of the lifecycle, which you have not it you treat the 'background' service as a pure POJO.

                        bax

                        • 9. Re: Running Java application inside JBoss
                          brolly

                          I'll give it a try tomorrow. So using XMBeans would only require adding a descriptor to the package, that might be a good solution for me, though now at this point I also don't find MBeans problematic at all. First I will probably use MBeans and then I'll look at XMBeans.

                          I'll try to find good posts about descriptors and about defining dependencies to MBeans then.

                          There's still one question no one answered, if I include a service jar inside a EAR file with the required descriptors can I by some way inform JBoss if I want that service to start or not to start, or will it allways start the service?

                          • 10. Re: Running Java application inside JBoss

                            The deployment descriptor says you want it deployed. There is no
                            "disabled" like with windows services. It follows the unix pattern, anything in the deploy
                            directory starts.

                            • 11. Re: Running Java application inside JBoss
                              brolly

                              ok, thank you. So if I want to disable a service deployed in my ear file and that is currently running I must delete that jar from the ear from what I understood.

                              Tomorrow I'll do a few tests if I run into some issue I'll ask help here at the forums.

                              • 12. Re: Running Java application inside JBoss
                                brolly

                                I got it working. Partially at least, the application starts but I have found a problem.

                                I need to access a datasource directly without referencing any EJB ( The datasource is already correctly defined in a -ds.xml file and I can access it through my EJBs).

                                Can I do it? If so how?
                                I've been trying with no success.

                                My code:

                                InitialContext ic = new InitialContext();
                                DataSource ds = (DataSource) ic.lookup("jdbc/eurDS");
                                dbConnection = ds.getConnection();

                                when I do the lookup I get a Naming Exception telling that jdc is not bound.
                                Do I need to specify any dependency on my SAR to the Datasource?

                                This code is exactly the same code I use to access the same datasource from my Session EJBs. I tryed to search help on this subject, but didn't find much.

                                • 13. Re: Running Java application inside JBoss
                                  brolly

                                  Nevermind, I was making a mistake, the code is working fine.