问题
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