Spring Integration - programmatically create / teardown channels

时光毁灭记忆、已成空白 提交于 2019-11-30 18:23:23

问题


Is it possible to programmatically create / teardown Spring Integration channels, as opposed to declaring them statically in Spring config?

My situation is this:

I have a webapp that requires push notifications, so I'm using long polling. Those long polling calls need to block on the server until they get an event.

What I'm envisioning (open to alternatives, of course), is a Spring Integration queue channel for each session, each listening to a single pubsub channel. So, when a server event happens it will be published to the pubsub, which will then pipe down to the individual session queues where they'll be popped off and sent to the client that's polling.

In order to do this, when a session is created I'd have to register new queue channel listing on the pubsub channel. When the session is destroyed, that new queue will also be destroyed.

Is something like this possible?

I don't want to have the the session listening directly to the pubsub because there's a risk of losing messages, so I want to have a queue in between to buffer them.

Thanks, Roy


回答1:


Yes, you can simply instantiate a QueueChannel and make it the outputChannel of a BridgeHandler. Then create an EventDrivenConsumer, passing the pubSub and bridge handler in the constructor.

Call start() on the consumer to make it subscribe to the pubSub, and stop() to unsubscribe when the session is closed.

If you don't want to hand-wire these components, simply declare the queue channel, and bridge in a small application context; make it a child of your main context and it will automatically subscribe to the pubSub in the parent context. Call context.destroy() to disconnect.



来源:https://stackoverflow.com/questions/20126706/spring-integration-programmatically-create-teardown-channels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!