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);
		
		messageSender.sendMessage(message.getBytes());
		
		messageSender.shutdown();
	}

}

@@ -2936,25 +2936,60 @@
 assword);%0A%09%09
-%0A
+int num=0;%0A%09%09try%7B%0A%09%09%09while(true)%7B%0A%09%09
 %09%09messageSen
@@ -3004,23 +3004,29 @@
 Message(
+(
 message
++num)
 .getByte
@@ -3033,17 +3033,167 @@
 s());%0A%09%09
-%0A
+%09%09System.out.println(%22%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF%E4%B8%BA:%22+message+num);%0A%09%09%09%09num++;%0A%09%09%09%09new Thread().sleep(60000);%0A%09%09%09%7D%0A%09%09%7Dcatch(Exception e)%7B%0A%09%09%09e.printStackTrace();%0A%09%09%7Dfinally%7B%0A%09
 %09%09messag
@@ -3208,18 +3208,22 @@
 shutdown();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Update RabbitMQMessageSender.java