0

我试图在同一个应用程序中将字符串消息从一个类传递到另一个类(从 Class1 到 Class2),所以我试图使用 Spring Integration 来解决它。

我的 context.xml 文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
      http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd" 

      xmlns:int="http://www.springframework.org/schema/integration"
      xmlns:int-jms="http://www.springframework.org/schema/integration/jms">

    <int:channel id="processEmpChannel">
        <int:queue/>
    </int:channel>

    <int-jms:inbound-channel-adapter
        channel="processEmpChannel" connection-factory="connectionFactory"
        destination-name="empQueue">
        <int:poller fixed-delay="500" />
    </int-jms:inbound-channel-adapter>

    <bean id="connectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="vm://localhost?broker.persistent=false" />
            </bean>
        </property>
        <property name="sessionCacheSize" value="10" />
        <property name="cacheProducers" value="false" />
    </bean>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>

    <bean id="springIntExample"
        class="com.distributed.analyzer.Manager">
        <property name="jmsTemplate" ref="jmsTemplate" />
    </bean>

    <int:service-activator input-channel="processEmpChannel" ref="springIntExample" method="processMessage">
        <int:poller fixed-delay="500"/>
    </int:service-activator>
</beans>

第一类:

package com.my.package;

public class Class1 implements Runnable {


        @Autowired
        private ApplicationContext appContext;
        private JmsTemplate jmsTemplate;

        ...

        public void someMethod() {
             Class1 c = (Class1) appContext.getBean("springIntExample");
             c.send();
        }

        public void send() {
            getJmsTemplate().convertAndSend("empQueue", "one message to test");
        }

        public JmsTemplate getJmsTemplate() {
            return jmsTemplate;
        }

        public void setJmsTemplate(JmsTemplate jmsTemplate) {
            this.jmsTemplate = jmsTemplate;
        }

        ...
    }

类 2 类:

package com.my.package;

public class Class2 implements Runnable {

    ...

    public void processMessage(String msg) {
         System.out.println("message recived: " + msg);
    }

    ...
}

我遇到的问题:

如果我打开我的 context.xml 文件,context.xml 中的以下行标有叉号(错误):

<int:poller fixed-delay="500" />

无论如何,我可以构建和执行应用程序,但在运行时会出现一条异常消息,指出 int:poller 中不允许使用 fixe-delay。

另外,我的实现是否正确?我不确定在解决问题后评论它是否会起作用。对不起,我是春天的新手。

4

0 回答 0