public void subscribe(Channel channel, String destination, String subscriptionId, String selector, boolean noLocal) { synchronized (this) { Subscription subscription = channel.addSubscription(destination, getId(), subscriptionId, noLocal); subscription.setSelector(selector); subscriptions.putIfAbsent(subscriptionId, subscription); } }
@Override public Channel removeChannel(String channelId, boolean timeout) { if (channelId == null) return null; Channel channel = (Channel)gaeCache.get(CHANNEL_PREFIX + channelId); if (channel != null) { for (Subscription subscription : channel.getSubscriptions()) { Message message = subscription.getUnsubscribeMessage(); handleMessage(channel.getFactory(), message, true); } channel.destroy(timeout); gaeCache.delete(CHANNEL_PREFIX + channelId); gaeCache.delete(GAEChannel.MSG_COUNT_PREFIX + channelId); } return channel; }
@Override public boolean equals(Object obj) { return (obj instanceof Channel && id.equals(((Channel)obj).getId())); }
@Override public final void run() { try { channel.runReceive(); } finally { reset(); // Make sure pending received messages that were enqueued during // runReceive() are going to be delivered ASAP (see issue GDS-1261). if (channel.hasReceivedMessage()) queue(channel.getGravity()); } } }
@Override public final void run() { try { channel.runPublish(); } finally { reset(); // Make sure pending published messages that were enqueued during // runPublish() are going to be delivered ASAP (see issue GDS-1261). if (channel.hasPublishedMessage()) queue(channel.getGravity()); } } }
public Set<Principal> getConnectedUsers() { Set<Principal> userPrincipals = new HashSet<Principal>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (timeChannel.getChannel().isConnected() && timeChannel.getChannel().getUserPrincipal() != null) userPrincipals.add(timeChannel.getChannel().getUserPrincipal()); } return userPrincipals; }
public Set<Principal> getConnectedUsersByDestination(String destination) { Set<Principal> userPrincipals = new HashSet<Principal>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (!timeChannel.getChannel().isConnected()) continue; for (Subscription subscription : timeChannel.getChannel().getSubscriptions()) { if (destination.equals(subscription.getDestination())) { userPrincipals.add(timeChannel.getChannel().getUserPrincipal()); break; } } } return userPrincipals; }
channel = channelFactory.newChannel(UUIDUtil.randomUUID(), clientType); TimeChannel<C> timeChannel = new TimeChannel<C>(channel); for (int i = 0; channels.putIfAbsent(channel.getId(), timeChannel) != null; i++) { if (i >= 10) throw new RuntimeException("Could not find random new clientId after 10 iterations"); channel.destroy(false); channel = channelFactory.newChannel(UUIDUtil.randomUUID(), clientType); timeChannel = new TimeChannel<C>(channel); String channelId = channel.getId();
if (!gdd.hasChannelId(channel.getId())) { gdd.addChannelId(channel.getId(), channel.getFactory().getClass().getName(), context.getClientType()); log.debug("Stored channel %s in distributed data", channel.getId()); log.debug("Saving new subscription message for channel: %s - %s", channel.getId(), message); gdd.addSubcription(channel.getId(), message); log.error(e, "Could not add subscription in distributed data: %s - %s", channel.getId(), subscriptionId); reply.setClientId(channel.getId()); reply.getHeaders().putAll(message.getHeaders()); gdd.setDestinationClientId(message.getDestination(), channel.getId()); gdd.setDestinationSubscriptionId(message.getDestination(), subscriptionId);
log.debug("Delivering JMS message to channel %s subscription %s", channel.getId(), subscriptionId); getGravity().initThread(null, channel.getClientType()); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(100); dmsg.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, subscriptionId); channel.receive(dmsg);
public List<Channel> getConnectedChannelsByDestination(String destination) { List<Channel> channels = new ArrayList<Channel>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (!timeChannel.getChannel().isConnected()) continue; for (Subscription subscription : timeChannel.getChannel().getSubscriptions()) { if (destination.equals(subscription.getDestination())) { channels.add(timeChannel.getChannel()); break; } } } return channels; }
public boolean deliver(Channel fromClient, AsyncMessage message) { if (!accept(fromClient, message)) return false; try { message.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, subscriptionId); log.debug("Channel %s deliver message to subscription %s", channel.getId(), subscriptionId); getChannel().receive(message); return true; } catch (MessageReceivingException e) { log.error(e, "Could not deliver message"); } return false; }
public void unsubscribe(Channel channel, String subscriptionId) { synchronized(this) { subscriptions.remove(subscriptionId); channel.removeSubscription(subscriptionId); } }
public Message publishMessage(Channel fromChannel, AsyncMessage message) { boolean shouldReleaseContext = GraniteContext.getCurrentInstance() == null; try { initThread(null, fromChannel != null ? fromChannel.getClientType() : serverChannel.getClientType()); return handlePublishMessage(null, message, fromChannel != null ? fromChannel : serverChannel); } finally { if (shouldReleaseContext) releaseThread(); } }
public void unsubscribe(CommandMessage message) throws Exception { String subscriptionId = (String)message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER); synchronized (consumers) { JMSConsumer consumer = consumers.get(subscriptionId); try { if (consumer != null) consumer.close(); } finally { consumers.remove(subscriptionId); channel.removeSubscription(subscriptionId); } } }
public List<Channel> findConnectedChannelsByUser(String name) { List<Channel> channels = new ArrayList<Channel>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (!timeChannel.getChannel().isConnected()) continue; if (timeChannel.getChannel().getUserPrincipal() != null && timeChannel.getChannel().getUserPrincipal().getName().equals(name)) channels.add(timeChannel.getChannel()); } return channels; }
public Message sendRequest(Channel fromChannel, AsyncMessage message) { boolean shouldReleaseContext = GraniteContext.getCurrentInstance() == null; try { initThread(null, fromChannel != null ? fromChannel.getClientType() : serverChannel.getClientType());
private void removeExpiredSubscriptions(Map<String, Subscription> subscriptions) { List<Object> channelIds = new ArrayList<Object>(subscriptions.size()); for (Subscription sub : subscriptions.values()) channelIds.add(GAEGravity.CHANNEL_PREFIX + sub.getChannel().getId()); Map<Object, Object> channels = gaeCache.getAll(channelIds); // Remove expired channel subscriptions for (Iterator<Map.Entry<String, Subscription>> ime = subscriptions.entrySet().iterator(); ime.hasNext(); ) { Map.Entry<String, Subscription> me = ime.next(); if (!channels.containsKey(GAEGravity.CHANNEL_PREFIX + me.getValue().getChannel().getId())) ime.remove(); } }
for (Subscription subscription : channel.getSubscriptions()) { try { Message message = subscription.getUnsubscribeMessage(); handleMessage(channel.getFactory(), message, true); channel.destroy(timeout);
public void unsubscribe(Channel channel, String subscriptionId) { // How to handle cluster synchronization ??? synchronized(this) { @SuppressWarnings("unchecked") Map<String, Subscription> subscriptions = (Map<String, Subscription>)gaeCache.get(TOPIC_PREFIX + getId()); if (subscriptions != null) { subscriptions.remove(subscriptionId); removeExpiredSubscriptions(subscriptions); } gaeCache.put(TOPIC_PREFIX + getId(), subscriptions); channel.removeSubscription(subscriptionId); } }