com.github.liyp.rabbitmq.demo2.Producer.java Source code

Java tutorial

Introduction

Here is the source code for com.github.liyp.rabbitmq.demo2.Producer.java

Source

/*
 * Copyright  2017 liyp (liyp.yunpeng@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.liyp.rabbitmq.demo2;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("liyp");
        factory.setPassword("liyp");
        factory.setVirtualHost("/");
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection conn = factory.newConnection();
        Channel channel = conn.createChannel();

        for (int i = 0; i < 10000; i++) {
            byte[] messageBodyBytes = "Hello, world!".getBytes();
            channel.basicPublish("", "my-queue", null, messageBodyBytes);
            System.out.println(channel.isOpen());
        }

        channel.close();
        conn.close();
    }
}