在 Spring 中以编程方式将 QueueChannel 桥接到 MessageChannel

发布于 2024-08-26 19:04:21 字数 914 浏览 5 评论 0原文

我正在尝试将队列连接到 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);

但我无法将它们放在一起。我缺少什么?

I'm attempting to wire a queue to the front of a MessageChannel, and I need to do so programatically so it can be done at run time in response to an osgi:listener being triggered. So far I've got:

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

}

Looking through the various relevant classes, I came up with:

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

    PollingConsumer pc = new PollingConsumer(qc, b);

but I'm not able to put it all together. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2024-09-02 19:04:21

因此,最终对我有用的解决方案是:

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"/>

So, the solution that ended up working for me was:

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

I'm not terribly clear if all the above is explicitly needed, but neither the trigger or the task scheduler alone worked, I did appear to need both. I should also note the taskSched used was the default taskScheduler dependency injected from spring via

<property name="taskSched" ref="taskScheduler"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文