Call a class before any other class in Quarkus- Camel

Viewed 265

I am working on Quarkus application, in which I am using Apache camel and in that I am using JAVA DSL way. I want to call RouteBuilder class after once specific class but RouteBuilder is not allowing that.

@Startup
@ApplicationScoped
public class FirstClass {
       void onStart(@Observes StartupEvent ev) {               
             //setting some values which are going to used in RouteBuilder class
       }
}



public class Rout extends RouteBuilder {

   @Override
   public void configure() throws Exception {
      // accessing value which is set on FirstClass but gives null pointer
   }
}

Now as you see while application startup I need to call FirstClass before RouteBuilder but everytime I start project RouteBuilder gets called first, how to call FirstClass First while starting apllication?

I have used @Startup, @PostConstrust but no luck, Please help me out as I am new to this.

1 Answers

You can use CDI to achieve this. For example:

@ApplicationScoped
public class FirstClass {
    public FirstClass {
        // Initialise data to be retrieved in the RouteBuilder
    }
}
@ApplicationScoped
public class Routes extends RouteBuilder {

    @Inject
    FirstClass firstClass;

    @Override
    public void configure() throws Exception {
        // Do something with FirstClass
    }
}

The camel-quarkus documentation provides some examples on how you can configure your application:

https://camel.apache.org/camel-quarkus/latest/user-guide/bootstrap.html

It's also worth familiarising yourself with how things can be wired together with CDI:

https://camel.apache.org/camel-quarkus/latest/user-guide/cdi.html

https://quarkus.io/guides/cdi-reference

Related