2017-04-24 117 views
0

我正在做一個非常簡單的Fuse集成應用程序,它從API網站(Stockholms Stad的數據門戶)獲取XML。我正在使用Apache Camel將「url」中的「url」從「 」路由到「新」word doc文件中。 2檔我這裏是Spring XML camel.context.xml文件和pom.xml的依賴關係:使用Apache Camel和Restlet獲取API

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 
    <camelContext id="camel-fuse-api" xmlns="http://camel.apache.org/schema/spring"> 
     <route id="api2fuse"> 
      <!-- Input from Rest API --> 
      <!-- Process the XML from the Rest API --> 
      <!-- Output to a text file (TESTING the route) --> 
      <from id="_from1" uri="restlet:http:80://data.stockholm.se/set/Befolkning/Befforandr/?apikey=<api-key>?restletMethod=GET"/> 
      <to id="_to1" uri="file://C:/<localfile>?fileName=test_new_xml.txt"/> 
      <log id="_log1" message="XML was copied to the local folder in a .txt file"/> 
     </route> 
    </camelContext> 
    <bean class="org.restlet.Component" id="RestletComponent"/> 
    <bean class="org.apache.camel.component.restlet.RestletComponent" id="RestletComponentService"> 
     <constructor-arg index="0"> 
      <ref bean="RestletComponent"/> 
     </constructor-arg> 
    </bean> 
</beans> 

pom.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.cgi.fuse</groupId> 
    <artifactId>camel-spring</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 
    <packaging>bundle</packaging> 
    <name>Fuse API</name> 
    <description>Fuse integration for API</description> 
    <licenses> 
    <license> 
     <name>Apache License, Version 2.0</name> 
     <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> 
     <distribution>repo</distribution> 
    </license> 
    </licenses> 
    <properties> 
    <camel.version>2.17.0.redhat-630187</camel.version> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <version.maven-bundle-plugin>2.1.0</version.maven-bundle-plugin> 
    <jboss.fuse.bom.version>6.3.0.redhat-187</jboss.fuse.bom.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <restlet.version>2.1.7</restlet.version> 
    </properties> 
    <dependencyManagement> 
    <dependencies> 
     <dependency> 
     <groupId>org.jboss.fuse.bom</groupId> 
     <artifactId>jboss-fuse-parent</artifactId> 
     <version>${jboss.fuse.bom.version}</version> 
     <type>pom</type> 
     <scope>import</scope> 
     </dependency> 
    </dependencies> 
    </dependencyManagement> 
    <dependencies> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-core</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-spring</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-stream</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-test-spring</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.activemq</groupId> 
     <artifactId>activemq-camel</artifactId> 
     <version>5.11.0.redhat-630187</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-jms</artifactId> 
     <version>2.17.0.redhat-630187</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-cxf</artifactId> 
     <version>2.17.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-restlet</artifactId> 
     <version>2.17.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.restlet.jee</groupId> 
     <artifactId>org.restlet.ext.spring</artifactId> 
     <version>2.2.0</version> 
    </dependency> 
    </dependencies> 
    <repositories> 
    <repository> 
     <releases> 
     <enabled>true</enabled> 
     <updatePolicy>never</updatePolicy> 
     </releases> 
     <snapshots> 
     <enabled>false</enabled> 
     </snapshots> 
     <id>fuse-public-repository</id> 
     <name>FuseSource Community Release Repository</name> 
     <url>https://repo.fusesource.com/nexus/content/groups/public</url> 
    </repository> 
    <repository> 
     <releases> 
     <enabled>true</enabled> 
     <updatePolicy>never</updatePolicy> 
     </releases> 
     <snapshots> 
     <enabled>false</enabled> 
     </snapshots> 
     <id>red-hat-ga-repository</id> 
     <name>Red Hat GA Repository</name> 
     <url>https://maven.repository.redhat.com/ga</url> 
    </repository> 
    <repository> 
    <id>maven-restlet</id> 
    <name>Public online Restlet repository</name> 
    <url>http://maven.restlet.org</url> 
</repository> 
    </repositories> 
    <pluginRepositories> 
    <pluginRepository> 
     <releases> 
     <enabled>true</enabled> 
     <updatePolicy>never</updatePolicy> 
     </releases> 
     <snapshots> 
     <enabled>false</enabled> 
     </snapshots> 
     <id>fuse-public-repository</id> 
     <name>FuseSource Community Release Repository</name> 
     <url>https://repo.fusesource.com/nexus/content/groups/public</url> 
    </pluginRepository> 
    <pluginRepository> 
     <releases> 
     <enabled>true</enabled> 
     <updatePolicy>never</updatePolicy> 
     </releases> 
     <snapshots> 
     <enabled>false</enabled> 
     </snapshots> 
     <id>red-hat-ga-repository</id> 
     <name>Red Hat GA Repository</name> 
     <url>https://maven.repository.redhat.com/ga</url> 
    </pluginRepository> 
    </pluginRepositories> 
    <build> 
    <defaultGoal>install</defaultGoal> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>maven-bundle-plugin</artifactId> 
     <version>${version.maven-bundle-plugin}</version> 
     <extensions>true</extensions> 
     <configuration> 
      <instructions> 
      <Bundle-SymbolicName>Fuse</Bundle-SymbolicName> 
      <Bundle-Name>Empty Camel Spring Example [Fuse]</Bundle-Name> 
      </instructions> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.5.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-resources-plugin</artifactId> 
     <version>3.0.1</version> 
     <configuration> 
      <encoding>UTF-8</encoding> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-maven-plugin</artifactId> 
     <version>${camel.version}</version> 
     <configuration> 
      <fileApplicationContextUri>src/main/resources/META-INF/spring/camel-context.xml</fileApplicationContextUri> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

我跑的「的jboss-保險絲進行項目JBoss Developer Studio 10.1.0 GA上的6.3.0.redhat-187「Runtime Server。我啓動服務器,但沒有創建文件。它不起作用,我認爲它在代碼中有一些錯誤的語法。

回答

0

如果您想調用外部現有REST服務,則需要從計時器開始。

from timer 
    to restlet 
    to file 

from restlet 

做什麼,如果你自己主持一個新的REST服務的目的。

隨着定時器,你需要找出你想調用現有的REST服務的頻率。計時器有可供您配置的選項。

+0

哦,我明白了。所以我所理解的是調用外部REST Web服務需要一個定時器來開始連接,監聽或輪詢正確嗎? – teowey

+0

是的,如果你正在使用駱駝路線。如果您想從Java代碼調用任何Camel端點,則可以使用ProducerTemplate(作爲客戶端API)。然而駱駝路線經常與駱駝一起使用 –

+0

ProducerTemplate文檔說它允許端點之間的消息交換。我對消息的概念很陌生。這是否意味着,如果我編寫Java代碼,我將不得不在我調用的API和本地主機端點之間實現消息交換?這是否需要,以便我可以生成我想要的新詞doc文件? – teowey