有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java、Spring和Apache中定义<beans>url

在Apache Camel中,我想将XML文件内容发送到jms队列。我的骆驼密码中有:

 .to("jms:accounting");

所以我需要在我的camel上下文中定义jms。xml

我有

 xmlns:broker="http://activemq.apache.org/schema/core"

然后

<!-- ActiveMQ Broker -->
<broker:broker useJmx="false" persistent="false" brokerName="localhost">
    <broker:transportConnectors>
        <broker:transportConnector name="tcp" uri="tcp://localhost:61616"/>
    </broker:transportConnectors>
</broker:broker>

<!-- JMS que -->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://localhost:61616"/>
        </bean>
    </property>
</bean>

当我运行该项目时,会出现以下错误:

[ERROR] Failed to execute goal org.apache.camel:camel-maven-plugin:2.20.0:run (default-cli) on project 08-xml-to-jms: null: MojoExecutionException: InvocationTargetException: Line 22 in XML document from file [/IdeaProjects/training/target/classes/META-INF/spring/camel-contxt.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 77; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'broker:broker'. -> [Help 1]

完整的POM文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.domain.subdomain</groupId>
    <artifactId>08-xml-to-jms</artifactId>
    <version>1.0-SNAPSHOT</version>



    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-spring -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>spi-annotations</artifactId>
            <version>2.20.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>2.20.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz</artifactId>
            <version>2.20.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.5</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- Allows the routes to be run via 'mvn camel:run' -->
            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>2.20.0</version>
            </plugin>
        </plugins>
    </build>

</project>

完整的骆驼上下文。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"
       xmlns:broker="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd">

    <!-- Currency Route -->
    <bean id="CurrencyRoute" class="com.domain.subdomain.route.CurrencyRoute">
        <property name="currencyWsURL" value="www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml" />
    </bean>

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="CurrencyRoute"/>
    </camelContext>


    <!-- ActiveMQ Broker -->
    <broker:broker useJmx="false" persistent="false" brokerName="localhost">
        <broker:transportConnectors>
            <broker:transportConnector name="tcp" uri="tcp://localhost:61616"/>
        </broker:transportConnectors>
    </broker:broker>

    <!-- JMS que -->
    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://localhost:61616"/>
            </bean>
        </property>
    </bean>

</beans>

完整的Java代码:

package com.domain.subdomain.route;

import org.apache.camel.builder.RouteBuilder;

public class CurrencyRoute extends RouteBuilder {


    private String currencyWsURL;



    @Override
    public void configure() {
        from("quartz://myTimer?trigger.repeatCount=0")
                .log("### Quartz trigger ###")
                .to("direct:readFile");

        from("direct:readFile")
                .log("### Read file ###")
                .to("https4://" + currencyWsURL)
                .to("jms:accounting");
                // .to("file:src/main/resources/data/work_in_progress?fileName=kursliste_ws-$simple{date:now:yyyyMMdd}.xml");
    }

    public void setCurrencyWsURL(String currencyWsURL) {
        this.currencyWsURL = currencyWsURL;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    我还发现代理模式必须在xsi:schemaLocation标记中定义

     <?xml version="1.0" encoding="UTF-8"?> <beans
     xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:broker="http://activemq.apache.org/schema/core"
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://activemq.apache.org/schema/core
            http://activemq.apache.org/schema/core/activemq-core.xsd
            http://camel.apache.org/schema/spring
            http://camel.apache.org/schema/spring/camel-spring.xsd">