private void createExchanges() throws IOException { Channel chan = channelPool.acquire(); try { chan.exchangeDeclare(BusExchange.NO_ROUTE.toString(), BusExchange.NO_ROUTE.getType()); Map<String, Object> args = map(e("alternate-exchange", (Object) BusExchange.NO_ROUTE.toString())); chan.exchangeDeclare(BusExchange.P2P.toString(), BusExchange.P2P.getType(), true, false, args); chan.exchangeDeclare(BusExchange.BROADCAST.toString(), BusExchange.BROADCAST.getType()); } finally { channelPool.returnChannel(chan); } }
private Map<String, Object> queueArguments() { Map<String, Object> ret = new HashMap<>(); ret.put("x-dead-letter-exchange", BusExchange.NO_ROUTE.toString()); ret.put("x-dead-letter-routing-key", CloudBus.HEADER_DEAD_LETTER); ret.put("x-expires", TimeUnit.MINUTES.toMillis(5)); return ret; } }
public void listen(Event evt, EventListenerWrapper l) { String type = evt.getType().toString(); try { synchronized (listeners) { List<EventListenerWrapper> lst = listeners.get(type); if (lst == null) { lst = new CopyOnWriteArrayList<EventListenerWrapper>(); listeners.put(type, lst); eventChan.queueBind(queueName, BusExchange.BROADCAST.toString(), type); logger.debug(String.format("[listening event]: %s", type)); } if (!lst.contains(l)) { lst.add(l); } } } catch (IOException e) { throw new CloudRuntimeException(e); } }
public void unlisten(Event evt, EventListenerWrapper l) { String type = evt.getType().toString(); try { synchronized (listeners) { List<EventListenerWrapper> lst = listeners.get(type); if (lst == null) { return; } lst.remove(l); if (lst.isEmpty()) { listeners.remove(type); eventChan.queueUnbind(queueName, BusExchange.BROADCAST.toString(), type); logger.debug(String.format("[unlistening event]: %s", type)); } } } catch (IOException e) { throw new CloudRuntimeException(e); } }
@Override public void run() throws Throwable { Channel chan = channelPool.acquire(); try { chan.queueUnbind(name, BusExchange.P2P.toString(), servId); } finally { channelPool.returnChannel(chan); } } });
@Override public void run() throws Throwable { Channel chan = channelPool.acquire(); try { chan.queueUnbind(name, BusExchange.BROADCAST.toString(), "#"); } finally { channelPool.returnChannel(chan); } } });
void send() throws IOException { try { chan.basicPublish(exchange.toString(), serviceId, true, msg.getAMQPProperties(), data); } catch (ShutdownSignalException e) { if (!(conn instanceof AutorecoveringConnection) || serverIps.size() <= 1 || !Platform.IS_RUNNING) { // the connection is not recoverable throw e; } logger.warn(String.format("failed to send a message because %s; as the connection is recoverable," + "we are doing recoverable send right now", e.getMessage())); if (!recoverSend()) { throw e; } } }
void trackService(String servId) { if (trackerClose) { return; } String[] pairs = servId.split("\\."); if (pairs.length < 2) { // don't track services not using management node id in service id return; } pairs[pairs.length-1] = "*"; String bindingKey = makeMessageQueueName(StringUtils.join(pairs, ".")); bindingKeys.add(bindingKey); if (logger.isTraceEnabled()) { logger.trace(String.format("message tracker binds to key[%s], tracking service[%s]", bindingKey, pairs[0])); } try { Channel chan = channelPool.acquire(); try { chan.queueBind(name, BusExchange.P2P.toString(), bindingKey); } finally { channelPool.returnChannel(chan); } } catch (IOException e) { throw new CloudRuntimeException(e); } }
void init() { try { ConnectionFactory connFactory = new ConnectionFactory(); List<Address> addresses = CollectionUtils.transformToList(bus.getServerIps(), new Function<Address, String>() { @Override public Address call(String arg) { return Address.parseAddress(arg); } }); conn = connFactory.newConnection(addresses.toArray(new Address[]{})); chan = conn.createChannel(); String name = MessageTracker.class.getName(); chan.queueDeclare(name, true, false, true, null); chan.basicConsume(name, true, this); chan.queueBind(name, BusExchange.P2P.toString(), "#"); chan.queueBind(name, BusExchange.BROADCAST.toString(), "#"); } catch (Exception e) { throw new CloudRuntimeException(e); } }
public void construct() { try { nrouteChan = conn.createChannel(); nrouteChan.queueDeclare(nrouteName, false, false, true, null); nrouteChan.queueBind(nrouteName, BusExchange.NO_ROUTE.toString(), ""); nrouteChan.basicConsume(nrouteName, true, this); } catch (IOException e) { throw new CloudRuntimeException(e); } }
chan.basicPublish(exchange.toString(), serviceId, true, msg.getAMQPProperties(), data); return true;
chan.queueDeclare(name, false, false, true, null); chan.basicConsume(name, true, tracker); chan.queueBind(name, BusExchange.BROADCAST.toString(), "#"); } finally { channelPool.returnChannel(chan);
chan.queueDeclare(outboundQueue.getName(), false, false, true, queueArguments()); chan.basicConsume(outboundQueue.getName(), true, consumer); chan.queueBind(outboundQueue.getName(), outboundQueue.getBusExchange().toString(), outboundQueue.getBindingKey()); channelPool.returnChannel(chan); maid.construct();