【问题标题】:Using Apache Camel and Restlet to get an API使用 Apache Camel 和 Restlet 获取 API
【发布时间】:2017-04-24 14:37:27
【问题描述】:

我正在做一个非常简单的 Fuse 集成应用程序,它从 API 网站(来自 Stockholms Stad 的数据门户)获取 XML。我正在使用 Apache Camel 路由“来自” “to”新单词文档文件中的 API url。我这里的 2 个文件是 Spring XML camel.context.xmlfile 和 pom.xml 的依赖项:

<?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 Developer Studio 10.1.0 GA 上的“jboss-fuse-6.3.0.redhat-187”运行时服务器上运行该项目。我启动服务器但没有创建文件。它不起作用,我认为代码中有一些错误的语法。

【问题讨论】:

    标签: xml maven apache-camel jbossfuse


    【解决方案1】:

    如果要调用外部现有 REST 服务,则需要从计时器开始。

    from timer
       to restlet
       to file
    

    你做什么

    from restlet
    

    适用于您自己托管新的 REST 服务。

    通过计时器,您需要找出调用现有 REST 服务的频率。计时器具有您可以为此配置的选项。

    【讨论】:

    • 哦,我明白了。所以我的理解是调用外部 REST Web 服务需要一个计时器来开始连接、侦听或轮询是否正确?
    • 是的,如果您使用骆驼路线。如果您想从 Java 代码调用任何 Camel 端点,您可以使用 ProducerTemplate(作为客户端 api)。然而,骆驼路线经常与骆驼一起使用
    • ProducerTemplate 文档说它允许端点之间的消息交换。我是消息传递概念的新手。这是否意味着,如果我编写 Java 代码,我将不得不在我正在调用的 API 和我的本地主机端点之间实现消息交换?是否需要这样才能生成我想要的新单词 doc 文件?
    • 您可以使用直接端点代替计时器,您可以使用 ProducerTemplate 从 Java 代码调用一次。然后你可以使用路由调用rest并保存为文件。这取决于你想做什么。例如问这个问题 - 是什么触发了这个文件的下载?它是来自 Java 代码的东西,还是应该在后台安排并自动运行等。
    • 是的,我仍然需要考虑我的项目的要求,看看它是按计划进行还是自动运行。但你的见解真的很有帮助。另外,我从 Camel in Action 中学到了很多东西。谢谢克劳斯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多