package com.crawler.docker_test;

import java.io.IOException;
import java.net.InetAddress;

import lombok.extern.slf4j.Slf4j;

import com.crawler.util.Config;
import com.rabbitmq.client.MessageProperties;

/**
 * RabbitMQ 队列消息发
 * 
 * @author shilei
 * 
 */
@Slf4j
public class RabbitMQMessageSender implements MessageSender {
	// RabbitMQ 队列连接
	private RabbitMQQueueConnecter connecter;

	public RabbitMQMessageSender(String brokers, String queueName) throws IOException {
		this(brokers, queueName, "guest", "guest");
	}

	public RabbitMQMessageSender(String brokers, String queueName, String user, String password) throws IOException {
		this(brokers, queueName, user, password, true);
	}

	public RabbitMQMessageSender(String brokers, String queueName, String user, String password, boolean durable)
			throws IOException {
		this(brokers, queueName, user, password, durable, new String[] { queueName });
	}

	public RabbitMQMessageSender(String brokers, String queueName, String user, String password, boolean durable,
			String[] routingKeys) throws IOException {
		// 1 建立连接
		connecter = new RabbitMQQueueConnecter(brokers, queueName);
		connecter.initConnection(user, password);
		connecter.initExchange("direct", durable);
		connecter.declareQueue(durable, true, routingKeys, null);
	}

	public void sendMessage(byte[] message, String routingKey) throws Exception {
		try {
			connecter.getChannel().basicPublish(connecter.getExchange(), routingKey, null, message); // 处理结束
		} catch (IOException e) {
			log.warn("RabbitMQ connect error when get message: ", e);
		}
	}

	/**
	 * 补充一个参数的方法
	 */
	public void sendMessage(byte[] message) throws Exception {
		try {
			// 修改第3个参数，使数据可以持久化
			connecter.getChannel().basicPublish(connecter.getExchange(), connecter.getQueueName(),
					MessageProperties.PERSISTENT_TEXT_PLAIN, message); // 处理结束
		} catch (IOException e) {
			log.warn("RabbitMQ connect error when get message: ", e);
		}

	}

	public void shutdown() {
		connecter.shutdown();
	}

	public static void main(String[] args) throws Exception {
		String ip=InetAddress.getByName(Config.getProperty("host")).getHostAddress();
		Integer port=Config.getIntProperty("port");
		String user="guest";
		String password="guest";
		String message = "http://item.jd.com/12345678.html";
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-ip")) {
				ip = args[++i];
		    }else if (args[i].equals("-port")) {
		        port = Integer.parseInt(args[++i]);
		    }else if (args[i].equals("-user")) {
		    	user = args[++i];
		    }else if (args[i].equals("-password")) {
		    	password = args[++i];
		    }else if (args[i].equals("-message")) {
		    	message = args[++i];
		    }
		}
		System.out.println("获取IP为："+ip);
		String brokers =ip +":"+port;
		
//		String brokers="10.1.235.30:10001";
		String queue = "LocalCrawlTaskQueue";

		MessageSender messageSender = new RabbitMQMessageSender(brokers, queue,user,password);
		int num=0;
		try{
			while(true){
				messageSender.sendMessage((message+num).getBytes());
				System.out.println("发送消息为:"+message+num);
				num++;
				new Thread().sleep(60000);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			messageSender.shutdown();
		}
	}

}

@@ -2054,16 +2054,32 @@
 ption %7B%0A
+%09%09//%E9%80%9A%E8%BF%87%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%8E%B7%E5%8F%96%E9%85%8D%E7%BD%AE%E9%A1%B9%0A
 %09%09String
@@ -2150,16 +2150,16 @@
 ress();%0A
-
 %09%09Intege
@@ -2301,16 +2301,32 @@
 .html%22;%0A
+%09%09//%E9%80%9A%E8%BF%87%E5%8F%82%E6%95%B0%E8%8E%B7%E5%8F%96%E8%8E%B7%E5%8F%96%E9%85%8D%E7%BD%AE%E9%A1%B9%0A
 %09%09for (i
@@ -2400,25 +2400,65 @@
 %09%09%09ip = 
-args%5B++i%5D
+InetAddress.getByName(args%5B++i%5D).getHostAddress()
 ;%0A%09%09    
@@ -2770,16 +2770,430 @@
   %7D%0A%09%09%7D%0A
+%09%09//%E9%80%9A%E8%BF%87%E7%B3%BB%E7%BB%9F%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%8E%B7%E5%8F%96%E9%85%8D%E7%BD%AE%E9%A1%B9%0A%09%09if(System.getenv(%22rabbitmq_ip%22)!=null)%0A%09%09%09ip=InetAddress.getByName(System.getenv(%22rabbitmq_ip%22)).getHostAddress();%0A%09%09if(System.getenv(%22rabbitmq_port%22)!=null)%0A%09%09%09port=Integer.parseInt(System.getenv(%22rabbitmq_port%22));%0A%09%09if(System.getenv(%22rabbitmq_user%22)!=null)%0A%09%09%09user=System.getenv(%22rabbitmq_user%22);%0A%09%09if(System.getenv(%22rabbitmq_password%22)!=null)%0A%09%09%09password=System.getenv(%22rabbitmq_password%22);%0A
 %09%09System
@@ -3702,14 +3702,13 @@
 ();%0A%09%09%7D%0A
+
 %09%7D%0A
-%0A
 %7D%0A

Update RabbitMQMessageSender.java