如何使用Spring Scheduler而不是骆驼计时器启动骆驼路线

时间:2019-10-11 10:44:02

标签: java spring-boot apache-camel

如何使用Spring Scheduler而不是计时器组件启动骆驼路线?

我已经尝试过使用骆驼计时器组件来触发路线,但是没有计时器可以使用Spring Scheduler来触发路线。

1)春季主班:-

@SpringBootApplication
public class SampleSchedulerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleSchedulerApplication.class, args);
    }
}

2)路由器类别:-

以下示例尝试了计时器组件。

//Directing to someService
from("timer://scheduler?period=10s")//What component should i use by default. 
.to("direct:someservice");

//Fetching datas from the rest api.
from("direct:someservice")                
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)              
.to("undertow:http://localhost:8090/api/employee/getemployees").
.log("Response : ${body}");

without timer, i can't able to trigger the route.

2 个答案:

答案 0 :(得分:0)

使用调度程序组件并将其配置为使用spring https://camel.apache.org/components/latest/scheduler-component.html

答案 1 :(得分:0)

我已经通过使用ProducerTemplate使用Spring Scheduler而不是Timer调用了骆驼路线 请参阅:https://camel.apache.org/manual/latest/producertemplate.html

1)Spring Scheduler:-

@Configuration
@EnableScheduling
public class SchedulerConfiguration {

    @Autowired
    private IntegrationService integrationService;

     @Scheduled(fixedDelay = 90000, initialDelay = 5000)
    public void integrationConfig() throws IOException {
        integrationService.getServiceAuthentication();

    }

2)集成服务;

@Component
public class IntegrationService {
    @Autowired
    private ProducerTemplate producerTemplate;

    public void getServiceAuthentication() {
 producerTemplate.sendBody("direct:someservice","username=123&password=123");
    }
}

3)Router Builder类;

 from("direct:someservice")                
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)              
.to("undertow:http://localhost:8090/api/employee/getemployees").
.log("Response : ${body}");