代码之家  ›  专栏  ›  技术社区  ›  Dusty

在Spring中通过编程将QueueChannel桥接到MessageChannel

  •  3
  • Dusty  · 技术社区  · 14 年前

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

    但我不能把所有的都放在一起。我错过了什么?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Dusty    14 年前

    所以,最终对我有效的解决方案是:

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