6 Replies Latest reply on Apr 19, 2011 1:22 AM by njiang

    Spawn a new thread in ServiceMix

    chungyeung

      Hi,

       

      I tried to integrate an application that needs to spawn new threads into ServiceMix.  I got a run time error that the pointer to the new thread is null.  Can somebody give me a link of an example how I can spawn a new thread in my application to run under ServiceMix?

       

      Thanks

       

      Chung-Yeung

        • 1. Re: Spawn a new thread in ServiceMix
          ffang

          Hi,

           

          You need elaborate which component you used in servicemix, the code how you create the thread, otherwise we can't give useful help.

           

          Freeman

          • 2. Re: Spawn a new thread in ServiceMix
            chungyeung

            Hi,

             

            Thank you for the reply.  I use ServiceMix version 4.3.1-fuse-01-09 together with Spring DM and camel.  The service basically waits for a message from a queue on "tcp.//localhost:61610" using camel component with the following defined route:

             

            <camel:route>

              <camel:from uri="jms:queue:testServer"/>

              <camel:to uri="testServer"/>

            </camel:route>

             

            The code for the testServer is as follows:

             

            @Service(value = "testServer")

            public class TestServerImpl implements TestServer, ApplicationContextAware {

              ....

              public Result getPrice(final String clientRef) {

                ....

                new Thread() {

                  @Override

                  public void run() {

                    ....

                    try {

                      Thread.sleep(500);

                       ....

                    } catch (..) {

                      ....

                    }

                  }

                }.start();

                ....

              }

              ....

            }

             

            I got the exception:

             

            Exception in thread "Thread-40" java.lang.NullPointerException at TestServiceImpl$2.run(TestServiceImpl.java:105)

             

            The position 105 is the code for the new thread.

             

            It would be great if you could show me how I can create a new thread without getting this exception.

             

            Thanks

             

            Chung-Yeung

            • 3. Re: Spawn a new thread in ServiceMix
              ffang

              Hi,

              You're using anonymous inner class

              new Thread() {

              @Override

              public void run() {

              //here throw NPE

              }.start();

              So exception in thread "Thread-40" java.lang.NullPointerException at TestServiceImpl$2.run(TestServiceImpl.java:105)

              means there's a NPE in your thread run method,  Line 105 here can't exactly point to the error line as you're using anonymous inner class .

              you need check your code in run method.

               

              Freeman

               

              Edited by: ffang on Apr 17, 2011 1:21 PM

              • 4. Re: Spawn a new thread in ServiceMix
                chungyeung

                Hi,

                 

                Thanks for the hint.  You are right.  It was not the thread.  The bean runs fine on a standalone application but throws an exception when it runs under a service in ServiceMix.  I found out that the problem was with the Spring application context that has not been set even though the bean was declared as implementation of ApplicationContextAware.  I had a similar implementation of a bean for ApplicationContextAware which I referred to from an external Jar in my service.   The application context was set for that bean.  May be I have missed something.  Should I start a new thread for this question?

                 

                Thanks

                • 5. Re: Spawn a new thread in ServiceMix
                  ffang

                  Hi,

                   

                  Yeah, start a new thread for a new question with appropriate question title is better.

                  You need post your whole bean.xml to that thread.

                   

                  Freeman

                  • 6. Re: Spawn a new thread in ServiceMix
                    njiang

                    Camel support the thread DSL[1] that you make take a look.

                    http://camel.apache.org/async.html

                     

                    Willem