基于注释的服务激活器在优先级队列中不使用自定义比较器

时间:2016-08-09 02:09:17

标签: spring annotations spring-integration

我有一个Spring Integration Queues的设置,将各种Java对象连接在一起。队列配置在spring-config.xml中设置,我将服务激活器作为Annotation。消息都很好,但根本没有使用自定义比较器。

<?xml version="1.0" encoding="UTF-8"?>
<beans 
xmlns:int="http://www.springframework.org/schema/integration"
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd">

<int:channel id="testGeneratedChannel">
    <int:priority-queue comparator="myComparator" capacity="500"/>
</int:channel>

<bean id="myComparator" class="com.my.server.myChannelComparitor"></bean>

</beans>

结束点注释如下:

@Configuration
@Service
public class TestEndPoint
{

    @Async("myOneAtATimeExecutor")
    @ServiceActivator(inputChannel = "testGeneratedChannel")
    public void consumeMessage(Message<Price> pr) {

        System.out.println("Timestamp: " + pr.getPayload().getTimestamp().format(DateTimeFormatter.ISO_DATE_TIME));
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

我尝试用所有Java重写整个配置,如本文(http://www.copperykeenclaws.com/configuring-spring-integration-channels-without-xml/)中所述,它可以正常工作。

@Configuration
public class IntegrationConfig {

    @Bean
    public PriorityChannel testGeneratedChannel() {
        return new PriorityChannel(new PriceChannelComparitor());
    }

    @Bean
    public TaskScheduler taskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

}

然后在主要课程中:

PriorityChannel channel = (PriorityChannel)     context.getBean(PriorityChannel.class);
    MessageHandler handler = new TestEndPoint();
    PollingConsumer consumer = new PollingConsumer(channel, handler);     
    consumer.setMaxMessagesPerPoll(1);
    PeriodicTrigger trigger = new PeriodicTrigger(10, TimeUnit.MILLISECONDS);
    consumer.setTrigger(trigger);
    consumer.setBeanFactory(context);    
    consumer.start();

然后将结束点更改为工具handleMessage

@Override
public void handleMessage(Message<?> arg0) throws MessagingException {

    Message<Price> pr = (Message<Price>) arg0;
    System.out.println("Timestamp: " + pr.getPayload().getTimestamp().format(DateTimeFormatter.ISO_DATE_TIME));
    // etc......
}

我看到的主要内容是纯Java方法和Annotation方法之间的区别是,有一个特定的Poller定义。查看文档http://docs.spring.io/spring-integration/reference/htmlsingle/#x4.0-poller-annotation似乎我应该能够在注释中设置Poller,但是每当我尝试时,我在STS中都会收到一条错误,说&#34;注释@Poller不允许这个位置&#34 ;。

有人可以建议使用基于注释的方法吗?

谢谢!

0 个答案:

没有答案