2017-04-24 133 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