org.apache.camel.FailedToStartRouteException: 由于空值原因,无法启动route1路由。

huangapple go评论139阅读模式
英文:

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

问题

@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
英文:

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

答案1

得分: 2

错误信息的最后一行是关键:

Caused by: java.lang.UnsupportedOperationException: 无法从 http 端点消费

[camel-http] 1 和 [camel-http4] 2(适用于 Camel 2.x)只能用作生产者,而不能用作消费者。

你需要使用例如 [camel-jetty] 3,它可以消费 http 请求

英文:

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.

答案2

得分: 0

以下是翻译好的内容:

我找到了答案。
Jetty将公开一个端点URL,生产者将使用该URL发送JSON/消息。因此,在生产者中添加Jetty公开的端点URL。
在pom.xml中添加camel-jetty-starter依赖项。
生产者:
假设您已经知道REST API将如何将消息发布到另一个API。因此,我只会分享来自控制器的postEntity代码。

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

消费者API路由:

@Component
public class HRFeedRoute extends RouteBuilder {

    @Override
    public final void configure() {
        System.out.println("从员工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("处理员工数据时出错:" + e.getMessage());
        }
    }
}
英文:

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());
}
}

}

huangapple
  • 本文由 发表于 2020年9月9日 19:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63811152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定