0
我嘗試使用camel實現一個簡單示例,其中我定義了使用jdbc和sql與數據庫交互的路由。 首先,我寫log4j.properties日誌:無法通過jdbc和Camel獲得JDBC連接
log4j.rootLogger=INFO, out
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
後,我在一個名爲sql.properties查詢寫:
sql.insertNewTopic=INSERT INTO newtopic(TopicId, TopicName, url, ModuleId, CreateDate) VALUES
(:#TopicId, :#TopicName, :#url, :#ModuleId, :#CreateDate)
## sql that select all unprocessed NewTopics
sql.selectNewTopic=select * from newtopic
## sql that update the NewTopic as being processed
sql.markNewTopic=update newtopic set TopicName = 'Apache Camel' where TopicId = :#TopicId
而且一個名爲applicationContext.xml文件,其中i定義豆設置數據庫和路線與它進行交互:稱爲在上述的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/javavill_forum" /> <!-- djdbc:mysql://localhost:3306/dbname -->
<property name="username" value="" />
<property name="password" value="" />
</bean>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="topicBean" class="com.mycompany.camelwithquartz.NewTopicBean" />
<!-- here is Camel configured with a number of routes -->
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- use Camel property placeholder loaded from the given file -->
<propertyPlaceholder id="placeholder" location="classpath:data/sql.properties" />
<!-- route that generate new orders and insert them in the database -->
<route id="generateOrder-route">
<from uri="timer:foo?period=5s" />
<transform>
<method ref="topicBean" method="generateNewTopic" />
</transform>
<to uri="sqlComponent:{{sql.insertNewTopic}}" />
<log message="Inserted new NewTopic ${body[TopicId]}" />
</route>
<!--
route that process the NewTopics by picking up new rows from the
database and when done processing then update the row to mark it as
processed
-->
<route id="processNewTopic-route">
<from uri="sqlComponent:{{sql.selectNewTopic}}?
consumer.onConsume={{sql.markNewTopic}}" />
<to uri="bean:topicBean?method=processNewTopic" />
<log message="${body}" />
<log message="Updated new NewTopic "/>
</route>
</camelContext>
</beans>
類NewTopicBean.java deines方法。 NewTopicBean.java如下所示:
package com.mycompany.camelwithquartz;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class NewTopicBean {
private Random ran = new Random();
public Map<String, Object> generateNewTopic() {
Map<String, Object> answer = new HashMap<String, Object>();
answer.put("TopicId", ran.nextInt());
answer.put("TopicName", "Camel in Action");
answer.put("url", "Camel in Action");
answer.put("ModuleId", ran.nextInt());
answer.put("CreateDate", new Date());
return answer;
}
/**
* Processes the NewTopic
*
* @param data the NewTopic as a {@link Map}
* @return the transformed NewTopic
*/
public String processNewTopic(Map<String, Object> data) {
return "Processed NewTopic id " + data.get("TopicId") + " TopicName "
+ data.get("TopicName")
+ " of " + data.get("ModuleId") + " copies of " + data.get("url");
}
}
最後我寫了一個類來測試它叫做TestQuartz;
public class TestQuartz {
static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(TestQuartz.class);
static final String pathLogger = "C:\\Users\\milioli\\Documents\\NetBeansProjects\\CamelWithQuartz\\src\\main\\resources\\data\\log4j.properties";
public static void main(String args[]) throws Exception {
PropertyConfigurator.configure(pathLogger);
logger.info("before to create app context with applicationContext.xml");
//AbstractApplicationContext context = new ClassPathXmlApplicationContext("C:\\Users\\milioli\\Documents\\NetBeansProjects\\CamelWithQuartz\\data\\applicationContext.xml");
AbstractApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/data/applicationContext.xml");
logger.info("after to creat app context with applicationContext.xml");
context.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Entered>>>>>");
context.stop();
}
}
pom.xml文件是正確的,生成項目,但是當我嘗試運行它,我得到這個異常:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure
的路由進行閱讀,但我不能給獲得連接。在applicationContext.xml中,我設置了// localhost:3306,它是默認端口,但它似乎不起作用。
有人可以幫助我嗎?
爲什麼用戶名和密碼都爲空? – StanislavL
@StanislavL因爲我試圖按照駱駝文檔的示例(http://camel.apache.org/sql-example.html) –
你可能想看看http://stackoverflow.com/questions/6865538/解決-A-通信鏈路故障,與-JDBC和MySQL的 –