Example usage for com.rabbitmq.client Channel getChannelNumber

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

Introduction

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

Prototype

int getChannelNumber();

Source Link

Document

Retrieve this channel's channel number.

Usage

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumer.java

License:Apache License

public void registerMessageHandler(MessageHandler messageHandler, boolean autoAck) {
    if (isRegistered.compareAndSet(false, true)) {
        try {//  w  w  w .  j  av  a 2 s .  c  om
            if (messageHandler instanceof AbstractRabbitMQMessageHandler) {
                String consumerTag = FlowContextFactory.getFlowContext() != null
                        ? FlowContextFactory.getFlowContext().getUniqueId()
                        : "N/A";
                Channel channel = RabbitMQMessagingFactory.getChannel();
                this.channelNumber = channel.getChannelNumber();
                ((AbstractRabbitMQMessageHandler) messageHandler).setChannelNumber(channelNumber);
                this.consumerTag = channel.basicConsume(queueName, autoAck, consumerTag,
                        (Consumer) messageHandler);
            } else {
                throw new IllegalArgumentException(
                        "Using RabbitMQ consumerThreadLocal you must provide a valid RabbitMQ massage handler");
            }

        } catch (IOException e) {
            //            LOGGER.error("can't register a MessageHandler: {}", e);
            throw new QueueException("can't register a MessageHandler: " + e, e);
        }
    }
}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumerWithoutTL.java

License:Apache License

RabbitMQMessageConsumerWithoutTL(String consumerName) {
    try {//w  w  w  . j a  v  a  2s  .c  o m
        this.consumerName = consumerName;
        Configuration subset = configuration.subset(consumerName);
        queueName = subset.getString("queue.name");

        String filter = subset.getString("queue.filter", "");
        boolean isAutoDelete = subset.getBoolean("queue.isAutoDelete", false);
        boolean isDurable = subset.getBoolean("queue.isDurable", true);
        boolean isSubscription = subset.getBoolean("queue.isSubscription", false);
        long expiration = subset.getLong("queue.expiration", 1800000);
        long maxLength = subset.getLong("queue.maxLength", -1);
        boolean deadLetterIsEnabled = subset.getBoolean("queue.deadLetterIsEnabled", true);
        String deadLetterExchangeName = subset.getString("queue.deadLetterExchangeName", DLQ);
        String subscribedTo = isSubscription ? subset.getString("queue.subscribedTo", "") : queueName;
        String exchangeType = subset.getString("queue.exchangeType", isSubscription ? "topic" : "direct");
        boolean isExclusive = subset.getBoolean("queue.isExclusive", false);
        try {
            RabbitMQMessagingFactory.INIT_LATCH.await();
        } catch (InterruptedException e) {
            LOGGER.error("error waiting for init to finish: " + e);
        }
        Channel channel = RabbitMQMessagingFactory.createChannelWithoutTL();
        channel.exchangeDeclare(subscribedTo, exchangeType, isDurable, false, false, null);

        Map<String, Object> args = new HashMap<String, Object>();

        if (maxLength > 0) {
            args.put("x-max-length", maxLength);
        }

        if (expiration > 0) {
            args.put("x-message-ttl", expiration);
        }

        if (deadLetterIsEnabled) {
            channel.exchangeDeclare(deadLetterExchangeName, exchangeType, isDurable, false, false, null);
            args.put("x-dead-letter-exchange", deadLetterExchangeName);
        }

        String queue = channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, args).getQueue();

        Map<String, String> filters = ConfigUtil.parseSimpleArrayAsMap(consumerName + ".queue.filters");
        if (filters != null && !filters.isEmpty()) {
            for (String routingKey : filters.values()) {
                channel.queueBind(queue, subscribedTo, routingKey);
            }
        } else {
            channel.queueBind(queue, subscribedTo, "#");
        }

        consumer = new QueueingConsumer(channel);
        //            channel.basicConsume(queueName, true, consumer);
        LOGGER.info("created rabbitmq consumer: {} on exchange: {}, queue-name: {} channel number {}",
                consumerName, subscribedTo, queueName, channel.getChannelNumber());
    } catch (IOException e) {
        throw new QueueException("Can't create consumer: " + e, e);
    }

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumerWithoutTL.java

License:Apache License

public void registerMessageHandler(MessageHandler messageHandler, boolean autoAck) {
    if (isRegistered.compareAndSet(false, true)) {
        try {//from w  w  w  .ja va  2  s. c om
            if (messageHandler instanceof AbstractRabbitMQMessageHandler) {
                String consumerTag = FlowContextFactory.getFlowContext() != null
                        ? FlowContextFactory.getFlowContext().getUniqueId()
                        : "N/A";
                Channel channel = consumer.getChannel();
                ((AbstractRabbitMQMessageHandler) messageHandler).setChannelNumber(channel.getChannelNumber());
                this.consumerTag = channel.basicConsume(queueName, autoAck, consumerTag,
                        (Consumer) messageHandler);
            } else {
                throw new IllegalArgumentException(
                        "Using RabbitMQ consumerThreadLocal you must provide a valid RabbitMQ massage handler");
            }

        } catch (IOException e) {
            //            LOGGER.error("can't register a MessageHandler: {}", e);
            throw new QueueException("can't register a MessageHandler: " + e, e);
        }
    }
}

From source file:com.cisco.oss.foundation.message.ChannelWrapper.java

License:Apache License

protected static void closeChannelAndRemoveFromMap(Channel channel, String reason) {

    // Note: once channel was closed, any action on the object will result with AlreadyClosedException :(
    try {/*from www  .j a  v  a  2s.c  o  m*/
        if (channel != null) {
            int channelNumber = channel.getChannelNumber();
            LOGGER.info("closeChannel " + channelNumber + " reason: " + reason);
            channel.close(200, reason);
            channels.remove(channelNumber);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    } catch (AlreadyClosedException e) {
        e.printStackTrace();
    }
}

From source file:com.cisco.oss.foundation.message.ChannelWrapper.java

License:Apache License

static Channel getChannel() {
    try {//  w  w w  .j  av a 2s  .  co  m
        if (channelThreadLocal.get() == null) {
            if (connection != null && connection.isOpen()) {
                Channel channel = connection.createChannel();
                channelThreadLocal.set(new ChannelWrapper(channel));
                channels.put(channel.getChannelNumber(), channel);
            } else {
                throw new QueueException("RabbitMQ appears to be down. Please try again later.");
            }
        }
        return channelThreadLocal.get().getChannel();
    } catch (IOException e) {
        throw new QueueException("can't create channel: " + e.toString(), e);
    }

}

From source file:com.cisco.oss.foundation.message.ChannelWrapper.java

License:Apache License

static Channel createChannelWithoutTL() {
    try {/* w w  w .  java  2s .  com*/
        if (connection != null && connection.isOpen()) {
            Channel channel = connection.createChannel();
            channels.put(channel.getChannelNumber(), channel);
            return channel;
        } else {
            throw new QueueException("RabbitMQ appears to be down. Please try again later.");
        }
    } catch (IOException e) {
        throw new QueueException("can't create channel: " + e.toString(), e);
    }
}

From source file:com.esri.geoevent.transport.rabbitmq.RabbitMQChannelListener.java

License:Apache License

@Override
public void onCreate(Channel channel) {
    notifyObservers(RabbitMQConnectionStatus.CREATED,
            LOGGER.translate("CHANNEL_CREATED", channel.getChannelNumber()), channel);
}

From source file:com.esri.geoevent.transport.rabbitmq.RabbitMQChannelListener.java

License:Apache License

@Override
public void onRecovery(Channel channel) {
    notifyObservers(RabbitMQConnectionStatus.RECOVERY,
            LOGGER.translate("CHANNEL_RECOVERED", channel.getChannelNumber()), channel);
}

From source file:com.esri.geoevent.transport.rabbitmq.RabbitMQChannelListener.java

License:Apache License

@Override
public void onRecoveryStarted(Channel channel) {
    notifyObservers(RabbitMQConnectionStatus.RECOVERY_STARTED,
            LOGGER.translate("CHANNEL_RECOVERY_STARTED", channel.getChannelNumber()), channel);
}

From source file:com.esri.geoevent.transport.rabbitmq.RabbitMQChannelListener.java

License:Apache License

@Override
public void onRecoveryCompleted(Channel channel) {
    notifyObservers(RabbitMQConnectionStatus.RECOVERY_COMPLETED,
            LOGGER.translate("CHANNEL_RECOVERY_COMPLETED", channel.getChannelNumber()), channel);
}