Return-Path: X-Original-To: apmail-camel-users-archive@www.apache.org Delivered-To: apmail-camel-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AC0BBF654 for ; Tue, 9 Apr 2013 13:49:50 +0000 (UTC) Received: (qmail 95953 invoked by uid 500); 9 Apr 2013 13:49:50 -0000 Delivered-To: apmail-camel-users-archive@camel.apache.org Received: (qmail 95851 invoked by uid 500); 9 Apr 2013 13:49:50 -0000 Mailing-List: contact users-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@camel.apache.org Delivered-To: mailing list users@camel.apache.org Received: (qmail 95821 invoked by uid 99); 9 Apr 2013 13:49:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Apr 2013 13:49:48 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of claus.ibsen@gmail.com designates 209.85.210.174 as permitted sender) Received: from [209.85.210.174] (HELO mail-ia0-f174.google.com) (209.85.210.174) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Apr 2013 13:49:44 +0000 Received: by mail-ia0-f174.google.com with SMTP id r13so16966iar.33 for ; Tue, 09 Apr 2013 06:49:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=x8qXqFC7//yoLPOLNwsGUA6XFtvf4Nwa3itcHPJF0aE=; b=PdyR7yyX2S6wmJ14/+A4ZmOwE0y5bPiYepQw/ddIyrUTfFkGTA8ZvDIHRw7P+C0tUU 6IdCXtULj9iJCNyHmiIc7FoaavAEVF44wP2Q3W2ol8UOR5ilOzYomJV+auZ+bhis65oU 8auYdKtAlC/hYuJxN+joBln9Xx6/l8wW5Pe8BcrORfs+cOZMB/26a0apiURHXbQ7wqc/ 3y3/8IIYXDP0d9GOoHf2FsSOYoFcsS/nGNB8wsiVyz/y13hHy6wTKphYu0Qk0kaO3wxZ sMPeqUsod3R550EnIw9nIBxfVo8jsAGWNHgN96tZKWM7d/axkRNHIw9611y4lxxfrmwq RePg== X-Received: by 10.50.100.167 with SMTP id ez7mr10094096igb.3.1365515364332; Tue, 09 Apr 2013 06:49:24 -0700 (PDT) MIME-Version: 1.0 Received: by 10.64.53.232 with HTTP; Tue, 9 Apr 2013 06:49:04 -0700 (PDT) In-Reply-To: References: From: Claus Ibsen Date: Tue, 9 Apr 2013 15:49:04 +0200 Message-ID: Subject: Re: Callback after startup from org.apache.camel.main.Main afterStart() To: "users@camel.apache.org" Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org Hi Yeah we probably need some API on MainSupport so you can add a custom EventNotifier, which we then add to the CamelContext before its started. Though another option that may be easier is just to have a beforeStart | beforeStop method which you can implement and do your custom code there. A bit like what you did, but IMHO should have simpler api's than a Callable et all. On Mon, Apr 8, 2013 at 6:34 PM, Andrew Bailey wrote: > Claus, thanks for the reply. > > Ok I tried that, however, if in the bootstrap code in main just before > calling run() I place > > List contexts=getCamelContexts(); > System.out.println(contexts); // [] > CamelContext context=contexts.get(0); > //IndexOutOfBoundsException: Index: 0, Size: 0 > context.getManagementStrategy().addEventNotifier(new > MyLoggingSentEventNotifer()); //as in > http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html > run(); > > It produces > [] > Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, > Size: 0 > > Is this a bug or am I doing something wrong? > > > > On Fri, Apr 5, 2013 at 9:24 AM, Claus Ibsen wrote: > >> Hi >> >> Yeah we could add some methods to the Main class you can override to >> have callbacks after start | before stop etc. >> But you can also just use the event notifier api in Camel for that. >> >> >> >> On Wed, Apr 3, 2013 at 4:17 PM, Andrew Bailey >> wrote: >> > I have a use case, where I require a fast startup time for camel and wish >> > to avoid classpath scanning. >> > I use the Main class however it appears that its not possible to obtain a >> > ProducerTemplate template = getCamelTemplate(); >> > before calling the run method. (it produces an ArrayOutOfBounds >> exception - >> > I could file a bug for that if you like) >> > >> > What I did was to extend Main (see code fragment below) and add a >> callback >> > called from the afterStart() method, so that user code can access the >> > context, and TypeConverterRegistry (as before that it is not >> initialized). >> > >> > Is there a better way to do it? >> > >> > If not and the developers think its a good idea, I would like to >> contribute >> > the code for a future release. >> > >> > >> > Thanks >> > >> > Andy >> > >> > >> > import org.apache.camel.main.Main; >> > >> > public class ServiceManager extends Main { >> > >> > >> > public static void main(String[] args) throws Exception { >> > ServiceManager example = new ServiceManager(); >> > example.boot(); >> > } >> > >> > public void boot() throws Exception { >> > //cut >> > >> > setAfterStartCallback(new Callable(){ >> > public Void call() throws Exception >> > { >> > ProducerTemplate template = getCamelTemplate(); >> > >> > fileMonitor.setProducer(template); >> > >> > /* >> > List contexts=getCamelContexts(); >> > System.out.println(contexts); >> > CamelContext context=contexts.get(0); >> > TypeConverterRegistry converterRegistry = >> > context.getTypeConverterRegistry(); >> > TypeConverter converter = >> > converterRegistry.lookup(String[].class, String.class); >> > System.out.println("Converter "+converter); >> > >> > TypeConverter converter2 = >> > converterRegistry.lookup(List.class, String.class); >> > System.out.println("Converter "+converter2); >> > >> > */ >> > return null; >> > } >> > }); >> > //code cut >> > System.out.println("Starting Camel. Use ctrl + c to terminate the >> > JVM.\n"); >> > run(); >> > } >> > >> > private Callable afterStartCallback; >> > >> > @Override >> > protected void afterStart() throws Exception { >> > >> > if (afterStartCallback!=null) afterStartCallback.call(); >> > } >> > >> > public Callable getAfterStartCallback() { >> > return afterStartCallback; >> > } >> > >> > public void setAfterStartCallback(Callable >> > afterStartCallback) { >> > this.afterStartCallback = afterStartCallback; >> > } >> > } >> > >> > -- >> > >> > >> > >> > Andrew Bailey >> > Cel - 312 866 95 56 >> > >> > http://www.apps4u.co >> > Chat Skype: apps4u >> > Twitter @apps4uco >> >> >> >> -- >> Claus Ibsen >> ----------------- >> Red Hat, Inc. >> FuseSource is now part of Red Hat >> Email: cibsen@redhat.com >> Web: http://fusesource.com >> Twitter: davsclaus >> Blog: http://davsclaus.com >> Author of Camel in Action: http://www.manning.com/ibsen >> > > > > -- > > > > Andrew Bailey > Cel - 312 866 95 56 > > http://www.apps4u.co > Chat Skype: apps4u > Twitter @apps4uco -- Claus Ibsen ----------------- Red Hat, Inc. FuseSource is now part of Red Hat Email: cibsen@redhat.com Web: http://fusesource.com Twitter: davsclaus Blog: http://davsclaus.com Author of Camel in Action: http://www.manning.com/ibsen