|
| 1 | +package org.springframework.integration.jdbc.channel; |
| 2 | + |
| 3 | +import java.util.concurrent.Executor; |
| 4 | + |
| 5 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 6 | +import org.springframework.integration.channel.AbstractSubscribableChannel; |
| 7 | +import org.springframework.integration.dispatcher.MessageDispatcher; |
| 8 | +import org.springframework.integration.dispatcher.UnicastingDispatcher; |
| 9 | +import org.springframework.integration.jdbc.store.JdbcChannelMessageStore; |
| 10 | +import org.springframework.messaging.Message; |
| 11 | +import org.springframework.messaging.MessageHandler; |
| 12 | + |
| 13 | +public class PostgresSubscribableChannel extends AbstractSubscribableChannel |
| 14 | +implements PostgresChannelMessageTableSubscriber.Subscription { |
| 15 | + |
| 16 | + private final JdbcChannelMessageStore jdbcChannelMessageStore; |
| 17 | + |
| 18 | + private final Object groupId; |
| 19 | + |
| 20 | + private final PostgresChannelMessageTableSubscriber messageTableSubscriber; |
| 21 | + |
| 22 | + private UnicastingDispatcher dispatcher = new UnicastingDispatcher(new SimpleAsyncTaskExecutor()); |
| 23 | + |
| 24 | + public PostgresSubscribableChannel(JdbcChannelMessageStore jdbcChannelMessageStore, |
| 25 | + Object groupId, PostgresChannelMessageTableSubscriber messageTableSubscriber) { |
| 26 | + |
| 27 | + this.jdbcChannelMessageStore = jdbcChannelMessageStore; |
| 28 | + this.groupId = groupId; |
| 29 | + this.messageTableSubscriber = messageTableSubscriber; |
| 30 | + } |
| 31 | + |
| 32 | + public void setDispatcherExecutor(Executor executor) { |
| 33 | + this.dispatcher = new UnicastingDispatcher(executor); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public boolean subscribe(MessageHandler handler) { |
| 38 | + boolean subscribed = super.subscribe(handler); |
| 39 | + if (this.dispatcher.getHandlerCount() == 1) { |
| 40 | + this.messageTableSubscriber.subscribe(this); |
| 41 | + notifyUpdate(); |
| 42 | + } |
| 43 | + return subscribed; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean unsubscribe(MessageHandler handle) { |
| 48 | + boolean unsubscribed = super.unsubscribe(handle); |
| 49 | + if (this.dispatcher.getHandlerCount() == 0) { |
| 50 | + this.messageTableSubscriber.unsubscribe(this); |
| 51 | + notifyUpdate(); |
| 52 | + } |
| 53 | + return unsubscribed; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected MessageDispatcher getDispatcher() { |
| 58 | + return this.dispatcher; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected boolean doSend(Message<?> message, long timeout) { |
| 63 | + this.jdbcChannelMessageStore.addMessageToGroup(this.groupId, message); |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void notifyUpdate() { |
| 69 | + Message<?> message; |
| 70 | + while ((message = this.jdbcChannelMessageStore.pollMessageFromGroup(this.groupId)) != null) { |
| 71 | + this.dispatcher.dispatch(message); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getRegion() { |
| 77 | + return this.jdbcChannelMessageStore.getRegion(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Object getGroupId() { |
| 82 | + return this.groupId; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments