Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to easily call a bean method after camel has started #324

Closed
davsclaus opened this issue May 12, 2020 · 6 comments
Closed

Add a way to easily call a bean method after camel has started #324

davsclaus opened this issue May 12, 2020 · 6 comments

Comments

@davsclaus
Copy link
Contributor

so you dont have to do the timer:foo?repeatCount=1 trick.

Some kind of annotation or convention method name or something. We do have some event listeners we can hook into. But lets consider making this easy for end users writing their camel k in a source file in java / groovy etc.

@lburgazzoli
Copy link
Contributor

lburgazzoli commented May 13, 2020

Wonder if we should do this through @BindToRegistry like:

@BindToRegistry
OnStart doOnStart() {
    return context -> doSomething(context);
}

so we can use the same thing also for groovy/kotlin

beans {
    bean<OnStart>("doOnStart") {
        doSomething(it)
    }
}

@davsclaus
Copy link
Contributor Author

Yeah possible. I was also considering something even easier, with just a special method name or something. As otherwise you have to juggle with all that lambda and annotations.

When all you wanted was just to have a method on RouteBuilder that camel-k would call.

public void onStartup() {
  ... here you can do whatever you want.
  .. you can access context
}

public void configure() {
   ... here is the route builder dsl
}

@lburgazzoli
Copy link
Contributor

lburgazzoli commented May 13, 2020

Maybe having an interface to extend ? I'm more keen to have a proper type to have better support for tooling so i.e. for plain Java you can do something like

class MyRoute extends RouteBuilder implements OnStartup {
    public void onStartup() {
    }
}

This would also allow the runtime to easily detect what to do without having to rely on reflection to access methods so when a route is added, the camel-k runtime can do something like:

if (route instanceof OnStartup) {
    context.addLifecycleStrategy(...);
}

and for scripting

context {
    onStartup {
        ...
        // this would directly add this closure body as handler
        // for the event by invoking context.addLifecycleStrategy(...)
    }
}

@davsclaus
Copy link
Contributor Author

The RouteBuilder could also have a onStartup method already so you just need to override it. But the interface is also a good way as its a very common pattern

@lburgazzoli
Copy link
Contributor

Or maybe we can just enrich the RouteBuilder DSL to have a onStartup, onShutdown method so you can write something like:

public void configure() {
   onStartup(this::doOnStartup);
}

That would be agnostic of the runtime

@lburgazzoli
Copy link
Contributor

Fixed on came by apache/camel#3850

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants