@Override public boolean equals(Object obj) { return (obj instanceof Channel && id.equals(((Channel)obj).getId())); }
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(); } }
public boolean accept(Channel fromClient, AsyncMessage message) { if (noLocal && message.getClientId().equals(channel.getId())) return false; return selector == null || selector.accept(message); }
private synchronized JMSClient connectJMSClient(Channel client, String destination) throws Exception { JMSClient jmsClient = jmsClients.get(client.getId()); if (jmsClient == null) { jmsClient = new JMSClientImpl(client); jmsClient.connect(); jmsClients.put(client.getId(), jmsClient); if (sessionSelector && GraniteContext.getCurrentInstance() instanceof ServletGraniteContext) ((ServletGraniteContext)GraniteContext.getCurrentInstance()).getSessionMap().put(JMSClient.JMSCLIENT_KEY_PREFIX + destination, jmsClient); log.debug("JMS client connected for channel " + client.getId()); } return jmsClient; }
private synchronized void closeJMSClientIfNecessary(Channel channel, String destination) throws Exception { JMSClient jmsClient = jmsClients.get(channel.getId()); if (jmsClient != null && !jmsClient.hasActiveConsumer()) { jmsClient.close(); jmsClients.remove(channel.getId()); if (sessionSelector && GraniteContext.getCurrentInstance() instanceof ServletGraniteContext) ((ServletGraniteContext)GraniteContext.getCurrentInstance()).getSessionMap().remove(JMSClient.JMSCLIENT_KEY_PREFIX + destination); log.debug("JMS client closed for channel " + channel.getId()); } }
public void setSelector(String selector) throws Exception { // To change the selector, just close the consumer but keep the session if (jmsConsumer != null) { jmsConsumer.close(); jmsConsumer = null; } connect(selector); log.debug("Changed selector to %s for JMS Consumer of channel %s", selector, channel.getId()); }
log.error(e, "Could not close JMS Producer for channel " + channel.getId()); log.error(e, "Could not close JMS Consumer " + consumer.subscriptionId + " for channel " + channel.getId()); log.error(e, "Could not close JMS Session for channel " + channel.getId()); log.debug(e, "Could not stop JMS Connection for channel " + channel.getId());
public void connect() throws ServiceException { if (jmsConnection != null) return; try { jmsConnection = jmsConnectionFactory.createConnection(); if (!useGlassFishNoExceptionListenerWorkaround) { try { jmsConnection.setExceptionListener(connectionExceptionListener); } catch (JMSException e) { if (e.getMessage().startsWith("MQJMSRA_DC2001: Unsupported:setExceptionListener()")) useGlassFishNoExceptionListenerWorkaround = true; else throw e; } } jmsConnection.start(); log.debug("JMS client connected for channel " + channel.getId()); } catch (JMSException e) { throw new ServiceException("JMS Initialize error", e); } }
private Message handlePingMessage(ChannelFactory<?> channelFactory, CommandMessage message) { Channel channel = createChannel(channelFactory, (String)message.getClientId()); AsyncMessage reply = new AcknowledgeMessage(message); reply.setClientId(channel.getId()); Map<String, Object> advice = new HashMap<String, Object>(); advice.put(RECONNECT_INTERVAL_MS_KEY, Long.valueOf(gravityConfig.getReconnectIntervalMillis())); advice.put(RECONNECT_MAX_ATTEMPTS_KEY, Long.valueOf(gravityConfig.getReconnectMaxAttempts())); advice.put(ENCODE_MESSAGE_BODY_KEY, Boolean.valueOf(gravityConfig.isEncodeMessageBody())); reply.setBody(advice); reply.setDestination(message.getDestination()); log.debug("handshake.handle: reply=%s", reply); return reply; }
private Message handleDisconnectMessage(final ChannelFactory<?> channelFactory, CommandMessage message) { Channel channel = getChannel(channelFactory, (String)message.getClientId()); if (channel == null) return handleUnknownClientMessage(message); removeChannel(channel.getId(), false); AcknowledgeMessage reply = new AcknowledgeMessage(message); reply.setDestination(message.getDestination()); reply.setClientId(channel.getId()); return reply; }
@Override protected void postManage(Channel channel) { Expiration expiration = Expiration.byDeltaMillis((int)getGravityConfig().getChannelIdleTimeoutMillis()); gaeCache.put(CHANNEL_PREFIX + channel.getId(), channel, expiration); } }
@Override protected <C extends Channel> C createChannel(ChannelFactory<C> channelFactory, String channelId) { C channel = null; if (channelId != null) { channel = getChannel(channelFactory, channelId); if (channel != null) return channel; } channel = channelFactory.newChannel(UUIDUtil.randomUUID(), null); Expiration expiration = Expiration.byDeltaMillis((int)getGravityConfig().getChannelIdleTimeoutMillis()); gaeCache.put(CHANNEL_PREFIX + channel.getId(), channel, expiration); gaeCache.put(GAEChannel.MSG_COUNT_PREFIX + channel.getId(), 0L, expiration); return channel; }
log.debug("Created JMS Producer for channel %s", 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);
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; }
DistributedData gdd = graniteConfig.getDistributedDataFactory().getInstance(); if (gdd != null) { log.debug("Removing subscription message from channel info: %s - %s", channel.getId(), subscriptionId); gdd.removeSubcription(channel.getId(), subscriptionId); log.error( e, "Could not remove subscription from distributed data: %s - %s", channel.getId(), message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER) ); reply.setClientId(channel.getId()); reply.getHeaders().putAll(message.getHeaders());
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"); String channelId = channel.getId();
public Message getUnsubscribeMessage() { CommandMessage unsubscribeMessage = new CommandMessage(); unsubscribeMessage.setOperation(CommandMessage.UNSUBSCRIBE_OPERATION); unsubscribeMessage.setClientId(getChannel().getId()); unsubscribeMessage.setDestination(destination); unsubscribeMessage.setHeader(AsyncMessage.SUBTOPIC_HEADER, getSubTopicId()); unsubscribeMessage.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, getSubscriptionId()); return unsubscribeMessage; }
reply.setClientId(fromChannel.getId());