2016-11-21 35 views
1

我使用Spring Boot來啓動使用Camel-sql查詢MySQL數據庫並調用REST服務的駱駝路由。駱駝SQL - 將數據源放入Spring Boot中的SimpleRegistry

application.properties中

db.driver=com.mysql.jdbc.Driver 
db.url=mysql://IP:PORT/abc 
db.username=abc 
db.password=pwd 

Application.java

public static void main(String[] args) { 
    SpringApplication.run(WlEventNotificationBatchApplication.class, args); 

} 

DataSourceConfig.java

public class DataSourceConfig { 

    @Value("${db.driver}") 
    public String dbDriver; 

    @Value("${db.url}") 
    public String dbUrl; 

    @Value("${db.username}") 
    public String dbUserName; 

    @Value("${db.password}") 
    public String dbPassword; 
    @Bean("dataSource") 
    public DataSource getConfig() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 

     dataSource.setDriverClassName(dbDriver); 
     dataSource.setUrl(dbUrl); 
     dataSource.setUsername(dbUserName); 
     dataSource.setPassword(dbPassword); 

     return dataSource; 
    } 
} 

WLRouteBuilder.java

@Component 
public class WLRouteBuilder extends RouteBuilder { 
    @Autowired 
    private NotificationConfig notificationConfig; 

    @Autowired 
    private DataSource dataSource; 

    @Override 
    public void configure() throws Exception { 

     from("direct:eventNotification") 
       .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource="+dataSource) 
       .process(new RowMapper()) 
       .log("${body}"); 

    } 
} 

我在運行時看到下面的錯誤,發現Camel無法在註冊表中找到DataSource bean。我很不確定如何在Spring Boot中使用Java DSL將「DataSource」注入到註冊表中。

?dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource%40765367 due to: No bean could be found in the registry for: [email protected]67 of type: javax.sql.DataSource 

回答

2

它是駱駝使用的URI,你是指它使用#語法記載這裏的bean的名稱:http://camel.apache.org/how-do-i-configure-endpoints.html(指豆類)

所以東西都

.to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#dataSource" 

其中dataSource是創建DataSource的bean的名稱,您可以給出其他名稱,例如

@Bean("myDataSource") 

然後駱駝SQL端點

.to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#myDataSource" 
+0

克勞斯你好,非常感謝您的回覆。我能再問你一個問題嗎? from(「sql:{{list.sql}}?dataSource =#dataSource」) .log(「process row $ {body}」) .end();在application.properties文件中,我包含了camel.springboot.main-run-controller = true。在我按下CNTRL + C之前,路由被無限執行。如果我刪除camel.springboot.main-run-controller,Camel啓動路由並停止主線程。無論如何,我可以讓駱駝開始,執行路線並停止。希望我的問題很明確。提前致謝! – user1637487

+0

看到這個常見問題:http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html –

+0

嗨克勞斯,對不起,如果我缺少一些非常簡單的東西。我爲這個應用程序使用Spring Boot。我試過(「sql:{{list.eventnotification.sql}}?dataSource =#dataSource」) .log(「process row $ {body}」) .to(「mock:bar」);並添加了getContext()。stop();.仍然路由不會因爲application.properties中的camel.springboot.main-run-controller = true而停止。在獨立的Java應用程序中,我們啓動route(camelcontext.start())並手動停止它。它在Spring Boot中如何工作?請幫助 – user1637487