Example usage for com.rabbitmq.client Channel addReturnListener

List of usage examples for com.rabbitmq.client Channel addReturnListener

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel addReturnListener.

Prototype

ReturnListener addReturnListener(ReturnCallback returnCallback);

Source Link

Document

Add a lambda-based ReturnListener .

Usage

From source file:org.mule.transport.amqp.internal.client.ChannelHandler.java

License:Open Source License

public Channel createChannel(ImmutableEndpoint endpoint) throws IOException {
    final AmqpConnector connector = (AmqpConnector) endpoint.getConnector();

    try {//from   w  w w. j  a  va  2  s  . c  o m
        final Channel channel = connector.getConnection().createChannel();
        channel.addReturnListener(connector.getDefaultReturnListener());
        channel.basicQos(connector.getPrefetchSize(), connector.getPrefetchCount(), false);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Created and configured new channel: " + channel);
        }

        channel.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(final ShutdownSignalException sse) {
                if (sse.isInitiatedByApplication()) {
                    return;
                }

                // do not inform the connector of the issue as it can't
                // decide what to do reset the channel so it would later
                // be lazily reconnected
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Terminated dead channel: " + channel, sse);
                }
            }
        });

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Shutdown listener configured on channel: " + channel);
        }

        return channel;
    } catch (final Exception e) {
        if ((!connector.isStopping()) && (connector.isStarted())) {
            connector.getMuleContext().getExceptionListener()
                    .handleException(new ConnectException(MessageFactory.createStaticMessage(
                            "Impossible to create new channels on connection: " + connector.getConnection()), e,
                            connector));
        }
        return null;
    }

}

From source file:org.mule.transport.amqp.internal.endpoint.dispatcher.Dispatcher.java

License:Open Source License

/**
 * Try to associate a return listener to the channel in order to allow flow-level exception
 * strategy to handle return messages.//from   w  w  w .  j av a2s  . com
 */
protected void addReturnListenerIfNeeded(final MuleEvent event, final Channel channel) {
    final ReturnListener returnListener = event.getMessage()
            .getInvocationProperty(AmqpConnector.RETURN_LISTENER);

    if (returnListener == null) {
        // no return listener defined in the flow that encompasses the event
        return;
    }

    if (returnListener instanceof DispatchingReturnListener) {
        ((DispatchingReturnListener) returnListener).setAmqpConnector(amqpConnector);
    }

    channel.addReturnListener(returnListener);

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Set return listener: %s on channel: %s", returnListener, channel));
    }
}

From source file:org.smartdeveloperhub.curator.connector.BrokerController.java

License:Apache License

private Channel createNewChannel() throws IOException {
    Preconditions.checkState(this.connection != null, "No connection available");
    final Channel result = this.connection.createChannel();
    Preconditions.checkState(result != null, "No channel available");
    result.addReturnListener(new LoggingReturnListener());
    return result;
}

From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorController.java

License:Apache License

void publishEvent(final String event, final String eventType) throws ControllerException {
    this.write.lock();
    try {/*from w ww  .  j a  v  a2  s . co m*/
        checkState(this.manager.isConnected(), "Not connected");
        final Channel aChannel = this.manager.currentChannel();
        aChannel.addReturnListener(new LoggingReturnListener());
        publish(aChannel, this.collector.getExchangeName(), Notifications.routingKey(eventType), event);
    } finally {
        this.write.unlock();
    }
}

From source file:org.springframework.amqp.rabbit.MulticastMain.java

License:Mozilla Public License

public static void main(String[] args) {
    Options options = getOptions();/*from  w  w  w  .  j  a  va  2s  .  com*/
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption('?')) {
            usage(options);
            System.exit(0);
        }

        String hostName = strArg(cmd, 'h', "localhost");
        int portNumber = intArg(cmd, 'p', AMQP.PROTOCOL.PORT);
        String exchangeType = strArg(cmd, 't', "direct");
        String exchangeName = strArg(cmd, 'e', exchangeType);
        int samplingInterval = intArg(cmd, 'i', 1);
        int rateLimit = intArg(cmd, 'r', 0);
        int producerCount = intArg(cmd, 'x', 1);
        int messageCount = intArg(cmd, 'N', 0);
        int consumerCount = intArg(cmd, 'y', 1);
        int connectionCount = cmd.hasOption('c') ? 1 : consumerCount;
        int producerTxSize = intArg(cmd, 'm', 0);
        int consumerTxSize = intArg(cmd, 'n', 0);
        boolean autoAck = cmd.hasOption('a');
        int prefetchCount = intArg(cmd, 'q', 0);
        int minMsgSize = intArg(cmd, 's', 0);
        int timeLimit = intArg(cmd, 'z', 0);
        List<String> flags = lstArg(cmd, 'f');
        int frameMax = intArg(cmd, 'M', 0);
        int heartbeat = intArg(cmd, 'b', 0);

        // setup
        String id = UUID.randomUUID().toString();
        Stats stats = new Stats(1000L * samplingInterval);
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(hostName);
        factory.setPort(portNumber);
        factory.setRequestedFrameMax(frameMax);
        factory.setRequestedHeartbeat(heartbeat);

        Connection[] consumerConnections = new Connection[connectionCount];
        for (int i = 0; i < connectionCount; i++) {
            Connection conn = factory.newConnection();
            consumerConnections[i] = conn;
        }
        Thread[] consumerThreads = new Thread[consumerCount];
        for (int i = 0; i < consumerCount; i++) {
            System.out.println("starting consumer #" + i);
            Connection conn = consumerConnections[i % connectionCount];
            Channel channel = conn.createChannel();
            if (consumerTxSize > 0)
                channel.txSelect();
            channel.exchangeDeclare(exchangeName, exchangeType);
            String queueName = channel.queueDeclare("", flags.contains("persistent"), true, false, null)
                    .getQueue();
            QueueingConsumer consumer = new QueueingConsumer(channel);
            if (prefetchCount > 0)
                channel.basicQos(prefetchCount);
            channel.basicConsume(queueName, autoAck, consumer);
            channel.queueBind(queueName, exchangeName, id);
            Thread t = new Thread(new Consumer(consumer, id, consumerTxSize, autoAck, stats, timeLimit));
            consumerThreads[i] = t;
            t.start();
        }
        Thread[] producerThreads = new Thread[producerCount];
        Connection[] producerConnections = new Connection[producerCount];
        for (int i = 0; i < producerCount; i++) {
            System.out.println("starting producer #" + i);
            Connection conn = factory.newConnection();
            producerConnections[i] = conn;
            Channel channel = conn.createChannel();
            if (producerTxSize > 0)
                channel.txSelect();
            channel.exchangeDeclare(exchangeName, exchangeType);
            final Producer p = new Producer(channel, exchangeName, id, flags, producerTxSize,
                    1000L * samplingInterval, rateLimit, minMsgSize, timeLimit, messageCount);
            channel.addReturnListener(p);
            Thread t = new Thread(p);
            producerThreads[i] = t;
            t.start();
        }

        for (int i = 0; i < producerCount; i++) {
            producerThreads[i].join();
            producerConnections[i].close();
        }

        for (int i = 0; i < consumerCount; i++) {
            consumerThreads[i].join();
        }
        for (int i = 0; i < connectionCount; i++) {
            consumerConnections[i].close();
        }

    } catch (ParseException exp) {
        System.err.println("Parsing failed. Reason: " + exp.getMessage());
        usage(options);
    } catch (Exception e) {
        System.err.println("Main thread caught exception: " + e);
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:vn.com.uet.performance.rabbitmq.MulticastParams.java

License:Open Source License

public Producer createProducer(Connection connection, Stats stats, String id) throws IOException {
    Channel channel = connection.createChannel();
    if (producerTxSize > 0)
        channel.txSelect();/*from ww w .  j av  a  2  s .  c o m*/
    if (confirm >= 0)
        channel.confirmSelect();
    if (!predeclared || !exchangeExists(connection, exchangeName)) {
        channel.exchangeDeclare(exchangeName, exchangeType);
    }
    final Producer producer = new Producer(channel, exchangeName, id, randomRoutingKey, flags, producerTxSize,
            producerRateLimit, producerMsgCount, minMsgSize, timeLimit, confirm, stats);
    channel.addReturnListener(producer);
    channel.addConfirmListener(producer);
    return producer;
}