3

我正在尝试将队列连接到 a 的前面MessageChannel,并且我需要以编程方式执行此操作,以便可以在运行时完成以响应osgi:listener被触发。到目前为止,我有:

public void addService(MessageChannel mc, Map<String,Object> properties)
{
    //Create the queue and the QueueChannel
    BlockingQueue<Message<?>> q = new LinkedBlockingQueue<Message<?>>();
    QueueChannel qc = new QueueChannel(q);

    //Create the Bridge and set the output to the input parameter channel
    BridgeHandler b = new BridgeHandler();
    b.setOutputChannel(mc);

    //Presumably, I need something here to poll the QueueChannel
    //and drop it onto the bridge.  This is where I get lost

}

浏览各种相关课程,我想出了:

    PollerMetadata pm = new PollerMetadata();
    pm.setTrigger(new IntervalTrigger(10));

    PollingConsumer pc = new PollingConsumer(qc, b);

但我无法将它们放在一起。我错过了什么?

4

1 回答 1

0

因此,最终为我工作的解决方案是:

public void addEngineService(MessageChannel mc, Map<String,Object> properties)
{
    //Create the queue and the QueueChannel
    BlockingQueue<Message<?>> q = new LinkedBlockingQueue<Message<?>>();
    QueueChannel qc = new QueueChannel(q);

    //Create the Bridge and set the output to the input parameter channel 
    BridgeHandler b = new BridgeHandler();
    b.setOutputChannel(mc);

    //Setup a Polling Consumer to poll the queue channel and 
    //retrieve 1 thing at a time
    PollingConsumer pc = new PollingConsumer(qc, b);
    pc.setMaxMessagesPerPoll(1);

    //Now use an interval trigger to poll every 10 ms and attach it
    IntervalTrigger trig = new IntervalTrigger(10, TimeUnit.MILLISECONDS);
    trig.setInitialDelay(0);
    trig.setFixedRate(true);
    pc.setTrigger(trig);

    //Now set a task scheduler and start it
    pc.setTaskScheduler(taskSched);
    pc.setAutoStartup(true);
    pc.start();
}

我不是很清楚是否明确需要以上所有内容,但是单独的触发器或任务调度程序都不起作用,我似乎确实需要两者。我还应该注意,使用的 taskSched 是从 spring 注入的默认 taskScheduler 依赖项

<property name="taskSched" ref="taskScheduler"/>
于 2010-04-05T14:42:21.083 回答