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();
		System.out.println("通过DNS："+Config.getProperty("host")+"获取IP为："+ip);
		String brokers =ip +":"+Config.getIntProperty("port");
		String queue = "LocalCrawlTaskQueue";

		String message = "http://item.jd.com/12345678.html";
		if(args.length>0)message=args[0];

		MessageSender messageSender = new RabbitMQMessageSender(brokers, queue);
		
		messageSender.sendMessage(message.getBytes());
		
		messageSender.shutdown();
	}

}

@@ -2140,36 +2140,21 @@
 ;%0A%09%09
-System.out.println(%22%E9%80%9A%E8%BF%87DNS%EF%BC%9A%22+
+Integer port=
 Conf
@@ -2151,32 +2151,35 @@
  port=Config.get
+Int
 Property(%22host%22)
@@ -2176,84 +2176,37 @@
 ty(%22
-host%22)+%22%E8%8E%B7%E5%8F%96IP%E4%B8%BA%EF%BC%9A%22+ip);%0A%09%09String brokers =ip +%22:%22+Config.getIntProperty(%22por
+port%22);%0A%09%09String user=%22gues
 t%22
-)
 ;%0A%09%09
@@ -2216,40 +2216,26 @@
 ing 
-queue = %22LocalCrawlTaskQueue
+password=%22guest
 %22;%0A
-%0A
 %09%09St
@@ -2289,42 +2289,569 @@
 l%22;%0A
+%09%09for (int i = 0; i %3C args.length; i++) %7B%0A%09
 %09%09if
+ 
 (args
-.length%3E0)message=args%5B0%5D
+%5Bi%5D.equals(%22-ip%22)) %7B%0A%09%09%09%09ip = args%5B++i%5D;%0A%09%09    %7Delse if (args%5Bi%5D.equals(%22-port%22)) %7B%0A%09%09        port = Integer.parseInt(args%5B++i%5D);%0A%09%09    %7Delse if (args%5Bi%5D.equals(%22-user%22)) %7B%0A%09%09    %09user = args%5B++i%5D;%0A%09%09    %7Delse if (args%5Bi%5D.equals(%22-password%22)) %7B%0A%09%09    %09password = args%5B++i%5D;%0A%09%09    %7Delse if (args%5Bi%5D.equals(%22-message%22)) %7B%0A%09%09    %09message = args%5B++i%5D;%0A%09%09    %7D%0A%09%09%7D%0A%09%09System.out.println(%22%E8%8E%B7%E5%8F%96IP%E4%B8%BA%EF%BC%9A%22+ip);%0A%09%09String brokers =ip +%22:%22+port;%0A%09%09%0A//%09%09String brokers=%2210.1.235.30:10001%22;%0A%09%09String queue = %22LocalCrawlTaskQueue%22
 ;%0A%0A%09
@@ -2921,16 +2921,30 @@
 s, queue
+,user,password
 );%0A%09%09%0A%09%09

Update RabbitMQMessageSender.java