Sri
Sri

Reputation: 71

org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of null

I am new to apache camel. I am trying to consume json, convert it into java object and write it in CSV file with pipe delimiter. However I am getting error while trying to consume the json from another api's endpoint. I am using spring boot to achieve this task.

Attached the sample code which I am trying.

@Component
public class HRFeedRoute extends RouteBuilder {

    @Override
    public final void configure() {
        System.out.println("fetching employee details from employee api");
        try {
            from("http://localhost:8083/getEmployee").startupOrder(1).to("direct:employee")
            .log("${body}");
                    
            from("direct:employee")
            .unmarshal().json(JsonLibrary.Jackson, Employee.class)
            .process(
                    ex -> {
                        System.out.println(ex);
                    });
            
        } catch(Error e) {
            System.out.println("Error occurred while processing employee data: "+e.getMessage());
        }
    }
}
org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of null
    at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:125)
    at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:263)
    at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:156)
    at org.apache.camel.impl.engine.InternalRouteStartupManager.doStartOrResumeRoutes(InternalRouteStartupManager.java:114)
    at org.apache.camel.impl.engine.AbstractCamelContext.doStartCamel(AbstractCamelContext.java:2809)
    at org.apache.camel.impl.engine.AbstractCamelContext.doStartContext(AbstractCamelContext.java:2657)
    at org.apache.camel.impl.engine.AbstractCamelContext.doStart(AbstractCamelContext.java:2620)
    at org.apache.camel.spring.boot.SpringBootCamelContext.doStart(SpringBootCamelContext.java:43)
    at org.apache.camel.support.service.BaseService.start(BaseService.java:115)
    at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2452)
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:121)
    at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:157)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at au.com.macquarie.hr.application.EmployeePackageBootApp.main(EmployeePackageBootApp.java:13)
Caused by: java.lang.UnsupportedOperationException: Cannot consume from http endpoint

Upvotes: 4

Views: 13522

Answers (2)

Sri
Sri

Reputation: 71

I found the answer. Jetty will expose an end point URL which will be used by the producer to send the json/message. So add the end poin url exposed by jetty in the producer. Add camel-jetty-starter dependency in the pom.xml Producer: Assuming that you are aware of how the rest api will post the message to another api. Hence, I am just sharing the postEntity code from the controller.

ResponseEntity responseEntity = restTemplate.postForEntity(new URI("http://localhost:8084/employee-package/getEmployee"), request, Employee.class);

Consumer API Router:

@Component public class HRFeedRoute extends RouteBuilder {

@Override
public final void configure() {
    System.out.println("fetching employee details from employee api");
    try {
        
        from("jetty://http://localhost:8084/employee-package/getEmployee")
        .log("${body}")
        .unmarshal().json(JsonLibrary.Jackson, Employee.class)
        .process(
                ex -> {
                    Employee employee = (Employee) ex.getIn().getBody();
                    System.out.println("|"+employee.getEmpId()+" | "+employee.getName()+" | "+employee.getDesignation()+" | "+employee.getSalary());
                });
        
    } catch(Error e) {
        System.out.println("Error occurred while processing employee data: "+e.getMessage());
    }
}

}

Upvotes: 0

burki
burki

Reputation: 7005

The last line of the error message is the key:

Caused by: java.lang.UnsupportedOperationException: Cannot consume from http endpoint

camel-http and camel-http4 (for Camel 2.x) are only usable as producer, but not as consumer.

You need to use for example camel-jetty that can consume http requests.

Upvotes: 2

Related Questions