Example usage for com.rabbitmq.client Connection createChannel

List of usage examples for com.rabbitmq.client Connection createChannel

Introduction

In this page you can find the example usage for com.rabbitmq.client Connection createChannel.

Prototype

Channel createChannel() throws IOException;

Source Link

Document

Create a new channel, using an internally allocated channel number.

Usage

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();//  w w  w .j  a va2  s.c om
    connection.close();
}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelCreatingThreads(final Connection connection, final String queueName,
        final boolean transactional) {
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(() -> {
            try {
                for (int t = 0; t < COMMIT_COUNT; t++) {
                    final Channel localChannel = connection.createChannel();
                    if (transactional) {
                        localChannel.txSelect();
                    }/*  www . j ava  2s .  co m*/
                    for (int j = 0; j < COMMIT_SIZE; j++) {
                        localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                    }
                    if (transactional) {
                        localChannel.txCommit();
                    }
                    localChannel.close();
                }
            } catch (IOException | TimeoutException e) {
                e.printStackTrace();
            }
        }));
    }
    return threads;
}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelSharingThreads(final Connection connection, final String queueName)
        throws IOException {
    List<Thread> threads = new ArrayList<>();
    final Channel localChannel = connection.createChannel();
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(() -> {
            try {
                for (int t = 0; t < COMMIT_COUNT; t++) {
                    for (int j = 0; j < COMMIT_SIZE; j++) {
                        localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                    }//from w  w  w  .jav  a 2  s . co m
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
    }
    return threads;
}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelPoolSharingThreads(final Connection connection,
        final String queueName) {
    List<Thread> threads = new ArrayList<>();
    final Queue<Channel> channels = new ArrayBlockingQueue<>(15);
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(() -> {
            try {
                for (int t = 0; t < COMMIT_COUNT; t++) {
                    Channel localChannel = channels.poll();
                    if (localChannel == null) {
                        localChannel = connection.createChannel();
                    }//w  w  w .j  a v a 2  s  .co m
                    localChannel.txSelect();
                    for (int j = 0; j < COMMIT_SIZE; j++) {
                        localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                    }
                    localChannel.txCommit();
                    if (!channels.offer(localChannel)) {
                        localChannel.close();
                    }
                }
            } catch (IOException | TimeoutException e) {
                e.printStackTrace();
            }
        }));
    }
    return threads;
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();//from  w  w  w. j a v a 2 s  .  c  o  m
    connection.close();
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelCreatingThreads(final Connection connection, final String queueName,
        final boolean transactional) {
    List<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(new Runnable() {
            @Override//from  ww  w .j  ava 2 s  .c  o m
            public void run() {
                try {
                    for (int t = 0; t < COMMIT_COUNT; t++) {
                        final Channel localChannel = connection.createChannel();
                        if (transactional) {
                            localChannel.txSelect();
                        }
                        for (int j = 0; j < COMMIT_SIZE; j++) {
                            localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                        }
                        if (transactional) {
                            localChannel.txCommit();
                        }
                        localChannel.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));
    }
    return threads;
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelSharingThreads(final Connection connection, final String queueName)
        throws IOException {
    List<Thread> threads = new ArrayList<Thread>();
    final Channel localChannel = connection.createChannel();
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(new Runnable() {
            @Override/*  w ww  . jav  a2  s  . co m*/
            public void run() {
                try {
                    for (int t = 0; t < COMMIT_COUNT; t++) {
                        for (int j = 0; j < COMMIT_SIZE; j++) {
                            localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));
    }
    return threads;
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

private static List<Thread> createChannelPoolSharingThreads(final Connection connection,
        final String queueName) {
    List<Thread> threads = new ArrayList<Thread>();
    final Queue<Channel> channels = new ArrayBlockingQueue<Channel>(15);
    for (int i = 0; i < THREAD_COUNT; i++) {
        threads.add(new Thread(new Runnable() {
            @Override/*ww w.j a v a 2  s  .c  o  m*/
            public void run() {
                try {
                    for (int t = 0; t < COMMIT_COUNT; t++) {
                        Channel localChannel = channels.poll();
                        if (localChannel == null) {
                            localChannel = connection.createChannel();
                        }
                        localChannel.txSelect();
                        for (int j = 0; j < COMMIT_SIZE; j++) {
                            localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8"));
                        }
                        localChannel.txCommit();
                        if (!channels.offer(localChannel)) {
                            localChannel.close();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));
    }
    return threads;
}

From source file:org.ballerinalang.messaging.rabbitmq.util.ChannelUtils.java

License:Open Source License

/**
 * Creates a RabbitMQ AMQ Channel./*from  ww  w.j  a  va2 s . c  o m*/
 *
 * @param connection RabbitMQ Connection object.
 * @return RabbitMQ Channel object.
 */
public static Channel createChannel(Connection connection) {
    try {
        return connection.createChannel();
    } catch (IOException exception) {
        String errorMessage = "An error occurred while creating the channel ";
        throw new BallerinaException(errorMessage + exception.getMessage(), exception);
    }
}

From source file:org.eclipse.hono.dispatcher.amqp.DefaultAmqpHelper.java

License:Open Source License

private DefaultAmqpHelper(final Connection connection, final QueueConfiguration queueConfiguration)
        throws IOException {
    this.connection = Objects.requireNonNull(connection);
    this.queueConfiguration = Objects.requireNonNull(queueConfiguration);

    channel = connection.createChannel();
    declareExchangeAndQueue();/* w  w w  .jav a  2 s.c  o m*/
}