1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| public class DefaultSubscriber<T> extends DefaultTopicParticipant implements Subscriber<T> { private static final Log log = LogFactory.getLog(DefaultSubscriber.class); private static final int DEFAULT_SHUTDOWN_TIMEOUT = 5; private static final TimeUnit DEFAULT_SHUTDOWN_TIMEOUT_UNITS = TimeUnit.SECONDS; private final NodeIdentifier nodeIdentifier; private final ScheduledExecutorService executorService; private final IncomingMessageQueue<T> incomingMessageQueue; private final Set<PublisherIdentifier> knownPublishers; private final TcpClientManager tcpClientManager; private final Object mutex;
private final ListenerGroup<SubscriberListener<T>> subscriberListeners; public static <S> DefaultSubscriber<S> newDefault(NodeIdentifier nodeIdentifier, TopicDeclaration description, ScheduledExecutorService executorService, MessageDeserializer<S> deserializer) { return new DefaultSubscriber<S>(nodeIdentifier, description, deserializer, executorService); } private DefaultSubscriber(NodeIdentifier nodeIdentifier, TopicDeclaration topicDeclaration, MessageDeserializer<T> deserializer, ScheduledExecutorService executorService) { super(topicDeclaration); this.nodeIdentifier = nodeIdentifier; this.executorService = executorService; incomingMessageQueue = new IncomingMessageQueue<T>(deserializer, executorService); knownPublishers = Sets.newHashSet(); tcpClientManager = new TcpClientManager(executorService); mutex = new Object(); SubscriberHandshakeHandler<T> subscriberHandshakeHandler = new SubscriberHandshakeHandler<T>(toDeclaration().toConnectionHeader(), incomingMessageQueue, executorService); tcpClientManager.addNamedChannelHandler(subscriberHandshakeHandler); subscriberListeners = new ListenerGroup<SubscriberListener<T>>(executorService); subscriberListeners.add(new DefaultSubscriberListener<T>() { @Override public void onMasterRegistrationSuccess(Subscriber<T> registrant) { log.info("Subscriber registered: " + DefaultSubscriber.this); } @Override public void onMasterRegistrationFailure(Subscriber<T> registrant) { log.info("Subscriber registration failed: " + DefaultSubscriber.this); } @Override public void onMasterUnregistrationSuccess(Subscriber<T> registrant) { log.info("Subscriber unregistered: " + DefaultSubscriber.this); } @Override public void onMasterUnregistrationFailure(Subscriber<T> registrant) { log.info("Subscriber unregistration failed: " + DefaultSubscriber.this); } }); } public SubscriberIdentifier toIdentifier() { return new SubscriberIdentifier(nodeIdentifier, getTopicDeclaration().getIdentifier()); } public SubscriberDeclaration toDeclaration() { return new SubscriberDeclaration(toIdentifier(), getTopicDeclaration()); } public Collection<String> getSupportedProtocols() { return ProtocolNames.SUPPORTED; } @Override public boolean getLatchMode() { return incomingMessageQueue.getLatchMode(); } @Override public void addMessageListener(MessageListener<T> messageListener, int limit) { incomingMessageQueue.addListener(messageListener, limit); } @Override public void addMessageListener(MessageListener<T> messageListener) { addMessageListener(messageListener, 1); } @Override public boolean removeMessageListener(MessageListener<T> messageListener) { return incomingMessageQueue.removeListener(messageListener); } @Override public void removeAllMessageListeners() { incomingMessageQueue.removeAllListeners(); } @VisibleForTesting public void addPublisher(PublisherIdentifier publisherIdentifier, InetSocketAddress address) { synchronized (mutex) { if (knownPublishers.contains(publisherIdentifier)) { return; } tcpClientManager.connect(toString(), address); knownPublishers.add(publisherIdentifier); signalOnNewPublisher(publisherIdentifier); } }
public void updatePublishers(Collection<PublisherIdentifier> publisherIdentifiers) { for (final PublisherIdentifier publisherIdentifier : publisherIdentifiers) { executorService.execute(new UpdatePublisherRunnable<T>(this, nodeIdentifier, publisherIdentifier)); } } @Override public void shutdown(long timeout, TimeUnit unit) { signalOnShutdown(timeout, unit); incomingMessageQueue.shutdown(); tcpClientManager.shutdown(); subscriberListeners.shutdown(); } @Override public void shutdown() { shutdown(DEFAULT_SHUTDOWN_TIMEOUT, DEFAULT_SHUTDOWN_TIMEOUT_UNITS); } @Override public void addSubscriberListener(SubscriberListener<T> listener) { subscriberListeners.add(listener); }
@Override public void signalOnMasterRegistrationSuccess() { final Subscriber<T> subscriber = this; subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onMasterRegistrationSuccess(subscriber); } }); }
@Override public void signalOnMasterRegistrationFailure() { final Subscriber<T> subscriber = this; subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onMasterRegistrationFailure(subscriber); } }); }
@Override public void signalOnMasterUnregistrationSuccess() { final Subscriber<T> subscriber = this; subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onMasterUnregistrationSuccess(subscriber); } }); }
@Override public void signalOnMasterUnregistrationFailure() { final Subscriber<T> subscriber = this; subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onMasterUnregistrationFailure(subscriber); } }); }
public void signalOnNewPublisher(final PublisherIdentifier publisherIdentifier) { final Subscriber<T> subscriber = this; subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onNewPublisher(subscriber, publisherIdentifier); } }); }
private void signalOnShutdown(long timeout, TimeUnit unit) { final Subscriber<T> subscriber = this; try { subscriberListeners.signal(new SignalRunnable<SubscriberListener<T>>() { @Override public void run(SubscriberListener<T> listener) { listener.onShutdown(subscriber); } }, timeout, unit); } catch (InterruptedException e) { } } @Override public String toString() { return "Subscriber<" + getTopicDeclaration() + ">"; } }
|