使用Spring集成在同一个应用程序中的两个类之间进行消息传递

时间:2016-02-08 21:25:24

标签: java spring spring-integration spring-messaging

我正在尝试将字符串消息从一个类传递到另一个类(从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>

Class Class1:

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;
        }

        ...
    }

Class Class2:

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。

另外,我的实现是否正确?我不确定在解决问题之后是否会起作用。我是春天的新人抱歉。

0 个答案:

没有答案