You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by bonnahu <bo...@gmail.com> on 2013/07/22 04:40:22 UTC

After camelContext shutdown, is any way to exit the Java main() method?

Hi guys,
I got another question here. I start my camelContext in the main(), then in
the route, I stop the camelContext 
in a Processor. Now I can see the camelContext is shutdown gracefully.
However, the Java application is still running. My question is that whether
there is a way to exit the Java main() automatically after the camelContext
is shutdown. 


public static void main(String[] args) throws Exception {
	String filename = "META-INF/spring/testingcontext.xml";
	Main main = new Main();
	main.setApplicationContextUri(filename);
	main.enableHangupSupport();
	main.run();
}


public class DownloadLogger implements Processor {
	public void process(final Exchange exchange) throws Exception {
    	System.out.println("We just downloaded:  " 
                + exchange.getIn().getHeader("CamelFileName"));
    	Thread stop = new Thread() {
                @Override
                public void run() {
                    try {
                        //exchange.getContext().stopRoute("myRoute");
                    
exchange.getContext().getShutdownStrategy().setTimeout(5);
                    	exchange.getContext().stop();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            };
        stop.start();
    	System.out.println("after camel context stop");
    }
}
I
thansk in advance!

Lei



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Paolo28 <pa...@gmail.com>.
Hi,
I´m not calling web service. In my case I had just file poller, splitter
with streaming (line by line processing using Camel Bindy), then aggregator
with parallelProcessing set true. Basically very simple flow. I wanted to
see the threads using JVVM but the JVM connection attempt always resulted in
sudden process terminat ion. 

I´m quite struggling with these lifecycle basics. The thing is that I´m
using Camel for implementing simple batch jobs - not my decision but still
it is convenient as there are many handy components and EIPs. This means I´m
not interested in running Camel as server/service which I can always stop
from operating system environment. How to properly write Main class with
main() method is becoming big excercise :-)
Currently I have working but quite ugly solution. I would like to ask
experienced Camel users how to implement simple 100% SEQUENTIAL and thus
deterministic flow (1-N tasks) where from performance reasons certain stage
or task should be done in parallel (something like
ExecutorService.invokeAll() - i.e. from client/caller perspective it is
synchronous call but behind the scenes the job is done quickly using N
threads). 

The flow is starting using ProducerTemplate - not by any poller etc.
When implementing batch you often need to know when all is done (no more
inflight messages, so you can verify results etc.) and then gracefully exit
the application using either zero or non-zero (errors) exit code.









--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739962.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: After camelContext shutdown, is any way to exit the Java main() method?

Posted by SyedBhai <sy...@hotmail.com>.
Hello,  If you are trying to invoke WSDL web service using Camel then this problem is solved I believe.  Kindly search the post titled "Is it Camel Bug?".  A JIRA was filled and the bug was removed.  If you are doing something else then please print the list of current threads in execution.  If one of the threads is started by Camel then that could be another bug in the Camel framework.
Syed.

Date: Wed, 18 Sep 2013 23:01:34 -0700
From: ml-node+s465427n5739807h26@n5.nabble.com
To: syedahmed.csse@hotmail.com
Subject: Re: After camelContext shutdown, is any way to exit the Java main() method?



	Did you try to take a thread dump to see what other threads may be

there to hang. Also mind that if you have non-deamon threads then a

JVM cannot terminate so easily without these threads being terminated

first.




On Thu, Sep 19, 2013 at 12:48 AM, Paolo28 <[hidden email]> wrote:

> I have the same problem.

>

> I have not found any solution to this issue. I believe that Camel should be

> able to shutdown itself in a graceful manner. My scenario:

>

> 1. process N messages (I´m able to detect which is the last)

> 2. once all messages are processed I want to gracefuly terminate the process

> 3. depending on the amount of possible errors I need to return non-zero exit

> status code

>

> What I do:

>

> - from a newly spawned thread I stop the initial route and then whole

> context using camelContext.stop()

> - this new thread dies - it is not hanging

> - I´m using 2.11

>

> Result:

>

> - all the "graceful shutdown stuff" is done (I see log messages) but the

> process is still running

> - I was even able to reliably reproduce weird behaviour: when running with

> INFO log level the camel java process exits as expected, however in case

> DEBUG log level it continues to run forever

> - also trying to connect via JVVM made the hanging process to finish with

> exit status=0 which is also a bit weird

> - except the "stopping thread" I do not create any of my own threads.

>

> I spent 2 days with various approaches and so far was not able to reliably

> stop Camel application.

> I would prefer some standard way - for example via control bus I would like

> to send command to stop everything (there are no more messages anyway). It

> seems like Camel in not really designed with this in mind.

>

>

>

>

>

>

>

>

>

> --

> View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739792.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 

Claus Ibsen

-----------------

Red Hat, Inc.

Email: [hidden email]

Twitter: davsclaus

Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen


	
	
	
	

	

	
	
		If you reply to this email, your message will be added to the discussion below:
		http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739807.html
	
	
		
		To unsubscribe from After camelContext shutdown, is any way to exit the Java main() method?, click here.

		NAML
	 		 	   		  



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739836.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Claus Ibsen <cl...@gmail.com>.
Did you try to take a thread dump to see what other threads may be
there to hang. Also mind that if you have non-deamon threads then a
JVM cannot terminate so easily without these threads being terminated
first.



On Thu, Sep 19, 2013 at 12:48 AM, Paolo28 <pa...@gmail.com> wrote:
> I have the same problem.
>
> I have not found any solution to this issue. I believe that Camel should be
> able to shutdown itself in a graceful manner. My scenario:
>
> 1. process N messages (I´m able to detect which is the last)
> 2. once all messages are processed I want to gracefuly terminate the process
> 3. depending on the amount of possible errors I need to return non-zero exit
> status code
>
> What I do:
>
> - from a newly spawned thread I stop the initial route and then whole
> context using camelContext.stop()
> - this new thread dies - it is not hanging
> - I´m using 2.11
>
> Result:
>
> - all the "graceful shutdown stuff" is done (I see log messages) but the
> process is still running
> - I was even able to reliably reproduce weird behaviour: when running with
> INFO log level the camel java process exits as expected, however in case
> DEBUG log level it continues to run forever
> - also trying to connect via JVVM made the hanging process to finish with
> exit status=0 which is also a bit weird
> - except the "stopping thread" I do not create any of my own threads.
>
> I spent 2 days with various approaches and so far was not able to reliably
> stop Camel application.
> I would prefer some standard way - for example via control bus I would like
> to send command to stop everything (there are no more messages anyway). It
> seems like Camel in not really designed with this in mind.
>
>
>
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739792.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Paolo28 <pa...@gmail.com>.
I have the same problem.

I have not found any solution to this issue. I believe that Camel should be
able to shutdown itself in a graceful manner. My scenario:

1. process N messages (I´m able to detect which is the last)
2. once all messages are processed I want to gracefuly terminate the process
3. depending on the amount of possible errors I need to return non-zero exit
status code 

What I do:

- from a newly spawned thread I stop the initial route and then whole
context using camelContext.stop()
- this new thread dies - it is not hanging
- I´m using 2.11

Result:

- all the "graceful shutdown stuff" is done (I see log messages) but the
process is still running
- I was even able to reliably reproduce weird behaviour: when running with
INFO log level the camel java process exits as expected, however in case
DEBUG log level it continues to run forever
- also trying to connect via JVVM made the hanging process to finish with
exit status=0 which is also a bit weird
- except the "stopping thread" I do not create any of my own threads. 

I spent 2 days with various approaches and so far was not able to reliably
stop Camel application. 
I would prefer some standard way - for example via control bus I would like
to send command to stop everything (there are no more messages anyway). It
seems like Camel in not really designed with this in mind. 









--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5739792.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by SyedBhai <sy...@hotmail.com>.
public static void main(String str[]) {
        try {
            CamelContext cc = new DefaultCamelContext();
            cc.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("direct:start1").
                           
to("cxf://http://localhost:8080/JAXWS1/MyInterfaceImpl?serviceClass=com.syed.MyInterface&serviceName={http://syed.com/}MyInterfaceImplService&portName={http://syed.com/}MyInterfaceImplPort"); 
// 
                }
            });

            cc.start();
            ProducerTemplate t = cc.createProducerTemplate();
            int annsal = t.requestBody("direct:start1", 10000,
Integer.class);
            System.out.println("annsal:" + annsal);
            //cc.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
            //cc.getShutdownStrategy().setTimeout(1);
            t.stop();
            cc.stop();
            //cc.getShutdownStrategy().setShutdownNowOnTimeout(true);
            //Thread.sleep(3000);
        } catch (Exception i) {
            i.printStackTrace();
        }
}



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736172.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Christian Posta <ch...@gmail.com>.
Do you want to post your complete main function?
As you've posted it, I cannot reproduce...


On Tue, Jul 23, 2013 at 2:39 PM, SyedBhai <sy...@hotmail.com>wrote:

> Hello Claus,
>   As per your instruction I removed all the statements that force the
> shutdown of camelcontext.
>   I let the camel take its time and shutdown gracefully.
>   To my surprise even though the default timeout is 300 secs I gave the
> application about ten minutes.  Still it did not shutdown and I had to
> forcibly terminate the program by clicking the close button.
>   I think this is a bug in camel framework.  I am using camel 2.11.0.
>   What is your idea?
>
> Thanks,
> Syed.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736152.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by SyedBhai <sy...@hotmail.com>.
Hello Claus,
  As per your instruction I removed all the statements that force the
shutdown of camelcontext.
  I let the camel take its time and shutdown gracefully.
  To my surprise even though the default timeout is 300 secs I gave the
application about ten minutes.  Still it did not shutdown and I had to
forcibly terminate the program by clicking the close button.
  I think this is a bug in camel framework.  I am using camel 2.11.0.
  What is your idea?

Thanks,
Syed.



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736152.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Claus Ibsen <cl...@gmail.com>.
You should give Camel time to shutdown graceful instead of forcing a 1
sec timeout
http://camel.apache.org/graceful-shutdown.html

On Tue, Jul 23, 2013 at 12:08 PM, SyedBhai <sy...@hotmail.com> wrote:
> Hi.
>   I am having a simple class with main().  In main() I am executing the
> following code:
> try {
>             CamelContext cc=new DefaultCamelContext();
>             cc.addRoutes(new RouteBuilder()  {
>                            public void configure()  {
>                                from("direct:start1").
>
> to("cxf://http://localhost:8080/JAXWS1/MyInterfaceImpl?serviceClass=com.syed.MyInterface&serviceName={http://syed.com/}MyInterfaceImplService&portName={http://syed.com/}MyInterfaceImplPort");
>                            }
>                          }
>                         );
>             cc.start();
>             ProducerTemplate t=cc.createProducerTemplate();
>             int annsal=t.requestBody("direct:start1", 10000, Integer.class);
>             System.out.println("annsal:"+annsal);
>             cc.getShutdownStrategy().setShutdownNowOnTimeout(true);
>             cc.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
>             cc.getShutdownStrategy().setTimeout(1);
>
>             t.stop();  cc.stop();
> }catch(Exception x){}
>
> To my surprise I am getting the output but the program is still running.  I
> later checked the list of threads running.  This is the list I have:
>
> Thread[main,5,main]
> Thread[Reference Handler,10,system]
> Thread[Finalizer,8,system]
> Thread[default-workqueue-1,5,default-workqueue]
> Thread[Thread-1,5,main]
> Thread[Signal Dispatcher,9,system]
> Thread[GC Daemon,2,system]
> Thread[I/O dispatcher 1,5,main]
> Thread[I/O dispatcher 2,5,main]
> Thread[I/O dispatcher 3,5,main]
> Thread[I/O dispatcher 4,5,main]
>
> If I use a mock:test endpoint instead of cxf the program terminates
> automatically.  So I believe when I am using some other components such as
> cxf then camel is creating some threads and not stopping them.
>
> This may cause memory leaks in my project.  Can you guys help?
>
> Thanks,
> Syed.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736105.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by SyedBhai <sy...@hotmail.com>.
Hi.
  I am having a simple class with main().  In main() I am executing the
following code:
try {
            CamelContext cc=new DefaultCamelContext();
            cc.addRoutes(new RouteBuilder()  {
                           public void configure()  {
                               from("direct:start1").
                                
to("cxf://http://localhost:8080/JAXWS1/MyInterfaceImpl?serviceClass=com.syed.MyInterface&serviceName={http://syed.com/}MyInterfaceImplService&portName={http://syed.com/}MyInterfaceImplPort");                             
                           }
                         }
                        );
            cc.start();
            ProducerTemplate t=cc.createProducerTemplate();
            int annsal=t.requestBody("direct:start1", 10000, Integer.class);
            System.out.println("annsal:"+annsal);
            cc.getShutdownStrategy().setShutdownNowOnTimeout(true);
            cc.getShutdownStrategy().setTimeUnit(TimeUnit.SECONDS);
            cc.getShutdownStrategy().setTimeout(1);
             
            t.stop();  cc.stop();
}catch(Exception x){}

To my surprise I am getting the output but the program is still running.  I
later checked the list of threads running.  This is the list I have:

Thread[main,5,main]
Thread[Reference Handler,10,system]
Thread[Finalizer,8,system]
Thread[default-workqueue-1,5,default-workqueue]
Thread[Thread-1,5,main]
Thread[Signal Dispatcher,9,system]
Thread[GC Daemon,2,system]
Thread[I/O dispatcher 1,5,main]
Thread[I/O dispatcher 2,5,main]
Thread[I/O dispatcher 3,5,main]
Thread[I/O dispatcher 4,5,main]

If I use a mock:test endpoint instead of cxf the program terminates
automatically.  So I believe when I am using some other components such as
cxf then camel is creating some threads and not stopping them.

This may cause memory leaks in my project.  Can you guys help?

Thanks,
Syed.



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736105.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Maybe there is a non-daemon thread still hanging around and preventing
the JVM from terminating.

You can do a thread dump to see which threads is alive.

But the system exit also works but just a bit more abrupt than a clean shutdown.

On Mon, Jul 22, 2013 at 5:47 PM, bonnahu <bo...@gmail.com> wrote:
> Hi Willem,
> After adding System.exit(1) after exchange.getContext().stop(); , it works
> for me. Thanks again!
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736042.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by bonnahu <bo...@gmail.com>.
Hi Willem,
After adding System.exit(1) after exchange.getContext().stop(); , it works
for me. Thanks again!



--
View this message in context: http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993p5736042.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: After camelContext shutdown, is any way to exit the Java main() method?

Posted by Willem Jiang <wi...@gmail.com>.
As you are using camel main to start the camel context. You need to call
the System.exit() when the Camel context is stopped.


Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/)
(English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem


On Mon, Jul 22, 2013 at 10:40 AM, bonnahu <bo...@gmail.com> wrote:

> Hi guys,
> I got another question here. I start my camelContext in the main(), then in
> the route, I stop the camelContext
> in a Processor. Now I can see the camelContext is shutdown gracefully.
> However, the Java application is still running. My question is that whether
> there is a way to exit the Java main() automatically after the camelContext
> is shutdown.
>
>
> public static void main(String[] args) throws Exception {
>         String filename = "META-INF/spring/testingcontext.xml";
>         Main main = new Main();
>         main.setApplicationContextUri(filename);
>         main.enableHangupSupport();
>         main.run();
> }
>
>
> public class DownloadLogger implements Processor {
>         public void process(final Exchange exchange) throws Exception {
>         System.out.println("We just downloaded:  "
>                 + exchange.getIn().getHeader("CamelFileName"));
>         Thread stop = new Thread() {
>                 @Override
>                 public void run() {
>                     try {
>                         //exchange.getContext().stopRoute("myRoute");
>
> exchange.getContext().getShutdownStrategy().setTimeout(5);
>                         exchange.getContext().stop();
>                     } catch (Exception e) {
>                         // ignore
>                     }
>                 }
>             };
>         stop.start();
>         System.out.println("after camel context stop");
>     }
> }
> I
> thansk in advance!
>
> Lei
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/After-camelContext-shutdown-is-any-way-to-exit-the-Java-main-method-tp5735993.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>