package com;

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

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

public class Producer {
//    private final static String EXCHANGE_NAME = "EXTAXIGPS";
//
//    public static void main(String[] args) throws Exception {
//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("121.17.184.59");
//        factory.setPort(50052);
//        factory.setUsername("guest");
//        factory.setPassword("guest");
//        Connection connection = factory.newConnection();
//        Channel channel = connection.createChannel();
//        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
//        String queueName = channel.queueDeclare().getQueue();
//        channel.queueBind(queueName, EXCHANGE_NAME, "");
//
//        Consumer consumer = new DefaultConsumer(channel) {
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                String message = new String(body, "UTF-8");
//                System.out.println("receive message :" + message);
//            }
//        };
//        channel.basicConsume(queueName, true, consumer);
//    }

//    private final static String QUEUE_NAME = "hello";
//
//    public static void main(String[] args) throws Exception {
//        // TODO Auto-generated method stub
//
//        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("121.17.184.59");
//        factory.setPort(50052);
//        factory.setUsername("guest");
//        factory.setPassword("guest");
//        Connection connection = null;
//        Channel channel = null;
//
//        connection = factory.newConnection();
//        channel = connection.createChannel();
//
//        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//        String message = "Hello World!";
//        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
//        System.out.println(" [Producer] Sent '" + message + "'");
//
//        channel.close();
//        connection.close();
//    }


    public final static String QUEUE_NAME="rabbitMQ.test4";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置RabbitMQ相关信息
        //factory.setHost("localhost") ;
        //factory.setHost("192.168.5.123");
        factory.setHost("121.17.184.59");
        factory.setUsername("admin");
        factory.setPassword("admin123");
        factory.setPort(50052);
        factory.setVirtualHost("/");
        //创建一个新的连接
        Connection connection = factory.newConnection();
        //创建一个通道
        Channel channel = connection.createChannel();
        //  声明一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello RabbitMQ";
        //发送消息到队列中
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println("Producer Send +'" + message + "'");
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}