當前位置: 首頁>>代碼示例>>Java>>正文


Java ConnectionFactory.setPort方法代碼示例

本文整理匯總了Java中com.rabbitmq.client.ConnectionFactory.setPort方法的典型用法代碼示例。如果您正苦於以下問題:Java ConnectionFactory.setPort方法的具體用法?Java ConnectionFactory.setPort怎麽用?Java ConnectionFactory.setPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.rabbitmq.client.ConnectionFactory的用法示例。


在下文中一共展示了ConnectionFactory.setPort方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setUsername("guest");
	factory.setPassword("guest");
	factory.setVirtualHost("/");
	factory.setHost("localhost");
	factory.setPort(5672);
	Connection newConnection = factory.newConnection();

	Channel channel = newConnection.createChannel();

	Scanner scanner = new Scanner(System.in);
	String message = "";
	while(!message.equals("exit")){
		System.out.println("Enter your message");
		message = scanner.next();
		channel.queueDeclare("flink-test", true, false, false, null);
		channel.basicPublish("", "flink-test", new BasicProperties.Builder()
				.correlationId(java.util.UUID.randomUUID().toString()).build(), message.getBytes());
	}
	
	scanner.close();
	channel.close();
	newConnection.close();
}
 
開發者ID:PacktPublishing,項目名稱:Practical-Real-time-Processing-and-Analytics,代碼行數:26,代碼來源:RMQPublisher.java

示例2: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws IOException
 * @throws TimeoutException
 * @date 2017年7月11日 下午5:53:02
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	
	Connection connection = factory.newConnection();
	Channel channel = connection.createChannel();
	channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
	// 分發信息
	for (int i = 0; i < 20; i++) {
		String message = "Hello RabbitMQ" + i;
		channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
		System.out.println("NewTask send '" + message + "'");
	}
	channel.close();
	connection.close();
}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:28,代碼來源:NewTask.java

示例3: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws TimeoutException
 * @throws IOException
 * @date 2017年7月13日 下午2:49:49
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {

	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	Connection connection = factory.newConnection();
	Channel channel = connection.createChannel();
	// 聲明交換機
	channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");// 注意是direct
	// 發送信息
	for (String routingKey : routingKeys) {
		String message = "RoutingSendDirect Send the message level:" + routingKey;
		channel.basicPublish(EXCHANGE_NAME_ROUTING, routingKey, null, message.getBytes());
		System.out.println("RoutingSendDirect Send" + routingKey + "':'" + message);
	}
	channel.close();
	connection.close();
}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:29,代碼來源:RoutingSendDirect.java

示例4: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException, TimeoutException {

        //建立連接工廠
        ConnectionFactory factory = new ConnectionFactory();
        //設置連接地址
        factory.setHost("seaof-153-125-234-173.jp-tokyo-10.arukascloud.io");
        factory.setPort(31084);
        //獲取連接
        Connection connection = factory.newConnection();
        //獲取渠道
        Channel channel = connection.createChannel();
        //聲明隊列,如果不存在就新建
        //參數1隊列名稱;參數2是否持久化;參數3排他性隊列,連接斷開自動刪除;參數4是否自動刪除;參數5.參數
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //發送的消息
        String message = Thread.currentThread().getName() + "Hello ";

        //參數1 交換機;參數2 路由鍵;參數3 基礎屬性;參數4 消息體
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(Thread.currentThread().getName() + "[send]" + message);
        channel.close();
        connection.close();

    }
 
開發者ID:liubo6,項目名稱:demo_springboot_rabbitmq,代碼行數:25,代碼來源:Send.java

示例5: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {

        //建立連接工廠
        ConnectionFactory factory = new ConnectionFactory();
        //設置連接地址
        factory.setHost("seaof-153-125-234-173.jp-tokyo-10.arukascloud.io");
        factory.setPort(31084);
        //獲取連接
        Connection connection = factory.newConnection();
        //獲取渠道
        Channel channel = connection.createChannel();

        //聲明交換機類型
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        //產生隨機數字
        String message = RandomStringUtils.randomNumeric(8);

        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());

        channel.close();
        connection.close();

    }
 
開發者ID:liubo6,項目名稱:demo_springboot_rabbitmq,代碼行數:25,代碼來源:EmitLog.java

示例6: consumeWithoutCertificate

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * Helper method to retrieve queue message from rabbitMQ
 *
 * @return result
 * @throws Exception
 */
private static String consumeWithoutCertificate() throws Exception {
    String result = "";
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5671);
    factory.useSslProtocol();

    Connection conn = factory.newConnection();
    Channel channel = conn.createChannel();

    GetResponse chResponse = channel.basicGet("WithoutClientCertQueue", true);
    if(chResponse != null) {
        byte[] body = chResponse.getBody();
        result = new String(body);
    }
    channel.close();
    conn.close();
    return result;
}
 
開發者ID:wso2,項目名稱:product-ei,代碼行數:26,代碼來源:ESBJAVA4569RabbiMQSSLStoreWithoutClientCertValidationTest.java

示例7: connection

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public static Connection connection() throws IOException, TimeoutException {
    if(connection == null) {
        synchronized(GabrielData.class) {
            if(connection != null) return connection;
            Config config = config();
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost(config.rabbitMQHost);
            connectionFactory.setPort(config.rabbitMQPort);
            connectionFactory.setUsername(config.rabbitMQUsername);
            connectionFactory.setPassword(config.rabbitMQPassword);
            connection = connectionFactory.newConnection();
            generalPurposeChannel = connection.createChannel();
            GatewayInfo.init(generalPurposeChannel);
        }
    }
    return connection;
}
 
開發者ID:natanbc,項目名稱:GabrielBot,代碼行數:18,代碼來源:GabrielData.java

示例8: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws TimeoutException
 * @throws IOException
 * @throws InterruptedException
 * @throws ConsumerCancelledException
 * @throws ShutdownSignalException
 */
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
		ConsumerCancelledException, InterruptedException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	Connection connection = factory.newConnection();
	Channel channel = connection.createChannel();
	channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
	channel.basicQos(1);
	QueueingConsumer consumer = new QueueingConsumer(channel);
	channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

	System.out.println("RPCServer Awating RPC request");
	while (true) {
		QueueingConsumer.Delivery delivery = consumer.nextDelivery();
		BasicProperties props = delivery.getProperties();
		BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(props.getCorrelationId())
				.build();

		String message = new String(delivery.getBody(), "UTF-8");
		int n = Integer.parseInt(message);

		System.out.println("RPCServer fib(" + message + ")");
		String response = "" + fib(n);
		channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());
		channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
	}
}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:40,代碼來源:RPCServer.java

示例9: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws IOException
 * @throws TimeoutException
 * @date 2017年7月13日 下午2:57:32
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	Connection connection = factory.newConnection();
	Channel channel = connection.createChannel();
	// 聲明交換器
	channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
	// 獲取匿名隊列名稱
	String queueName = channel.queueDeclare().getQueue();
	// 根據路由關鍵字進行多重綁定
	for (String severity : routingKeys2) {
		channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, severity);
		System.out.println("ReceiveLogsDirect2 exchange:" + EXCHANGE_NAME_ROUTING + ", queue:" + queueName
				+ ", BindRoutingKey:" + severity);
	}
	System.out.println("ReceiveLogsDirect2 Waiting for messages");

	Consumer consumer = new DefaultConsumer(channel) {
		@Override
		public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
				byte[] body) throws UnsupportedEncodingException {
			String message = new String(body, "UTF-8");
			System.out.println("ReceiveLogsDirect2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
		}
	};
	channel.basicConsume(queueName, true, consumer);

}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:40,代碼來源:ReceiveLogsDirect2.java

示例10: connectToBroker

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public void connectToBroker(){
	try {
		/*Get a ConnectionFactory object */
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(ip);
		factory.setPort(port);
		factory.setUsername(user);
		factory.setPassword(password);
		/* Create a connection */
		connection = factory.newConnection();
		/* Create a channel over that TCP/IP connection */
		channel = connection.createChannel();
	}
	catch(Exception e){
		e.printStackTrace();
	}
}
 
開發者ID:hemantverma1,項目名稱:ServerlessPlatform,代碼行數:18,代碼來源:communicateWithMQ.java

示例11: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws TimeoutException
 * @throws IOException
 * @date 2017年7月11日 下午5:21:46
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	Connection connection = null;
	Channel channel = null;
	try {
		// 創建一個新的連接
		connection = factory.newConnection();
		// 創建一個通道
		channel = connection.createChannel();
		// 聲明一個隊列
		// queueDeclare第一個參數表示隊列名稱
		//第二個參數為是否持久化(true表示是,隊列將在服務器重啟時生存)
		//第三個參數為是否是獨占隊列(創建者可以使用的私有隊列,斷開後自動刪除)
		//第四個參數為當所有消費者客戶端連接斷開時是否自動刪除隊列
		//第五個參數為隊列的其他參數
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		String message = "{\"temperature\":100}";
		// 發送消息到隊列中
		//basicPublish第一個參數為交換機名稱
		//第二個參數為隊列映射的路由key
		//第三個參數為消息的其他屬性
		//第四個參數為發送信息的主體
		channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
		System.out.println("Producer Send +'" + message + "'");

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		// 關閉通道和連接
		channel.close();
		connection.close();
	}
}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:46,代碼來源:Producer.java

示例12: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws TimeoutException
 * @throws IOException
 * @date 2017年7月11日 下午5:32:45
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {
	// 創建連接工廠
	ConnectionFactory factory = new ConnectionFactory();
	// 設置RabbitMQ地址
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	// 創建一個新的連接
	Connection connection = factory.newConnection();
	// 創建一個通道
	Channel channel = connection.createChannel();
	// 聲明要關注的隊列
	channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	System.out.println("Customer Waiting Received messages");
	// DefaultConsumer類實現了Consumer接口,通過傳入一個頻道,
	// 告訴服務器我們需要那個頻道的消息,如果頻道中有消息,就會執行回調函數handleDelivery
	Consumer consumer = new DefaultConsumer(channel) {
		//envelope主要存放生產者相關信息(比如交換機、路由key等)
		//body是消息實體
		@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("Customer Received '" + message + "'");
		}
	};
	// 自動回複隊列應答 -- RabbitMQ中的消息確認機製
	channel.basicConsume(QUEUE_NAME, true, consumer);
}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:39,代碼來源:Customer.java

示例13: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
	 * @param args
	 * @throws TimeoutException 
	 * @throws IOException 
	 * @date 2017年7月13日 下午3:08:40
	 * @writer junehappylove
	 */
	public static void main(String[] args) throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(host);
		factory.setUsername(username);
		factory.setPassword(password);
		factory.setPort(port);
		factory.setVirtualHost(virtualHost);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
//      聲明一個匹配模式的交換器
        channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
        String queueName = channel.queueDeclare().getQueue();
        // 路由關鍵字
        String[] routingKeys = new String[]{"*.*.rabbit", "lazy.#"};
//      綁定路由關鍵字
        for (String bindingKey : routingKeys) {
            channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, bindingKey);
            System.out.println("ReceiveLogsTopic2 exchange:"+EXCHANGE_NAME_TOPIC+", queue:"+queueName+", BindRoutingKey:" + bindingKey);
        }

        System.out.println("ReceiveLogsTopic2 Waiting for messages");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
					byte[] body) throws UnsupportedEncodingException  {
                String message = new String(body, "UTF-8");
                System.out.println("ReceiveLogsTopic2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            }
        };
        channel.basicConsume(queueName, true, consumer);
	}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:40,代碼來源:ReceiveLogsTopic2.java

示例14: main

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
 * @param args
 * @throws IOException
 * @throws TimeoutException
 * @date 2017年7月13日 下午3:06:20
 * @writer junehappylove
 */
public static void main(String[] args) throws IOException, TimeoutException {
	ConnectionFactory factory = new ConnectionFactory();
	factory.setHost(host);
	factory.setUsername(username);
	factory.setPassword(password);
	factory.setPort(port);
	factory.setVirtualHost(virtualHost);
	Connection connection = factory.newConnection();
	Channel channel = connection.createChannel();

	// 聲明一個匹配模式的交換機
	channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
	String queueName = channel.queueDeclare().getQueue();
	// 路由關鍵字
	String[] routingKeys = new String[] { "*.orange.*" };
	// 綁定路由
	for (String routingKey : routingKeys) {
		channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, routingKey);
		System.out.println("ReceiveLogsTopic1 exchange:" + EXCHANGE_NAME_TOPIC + ", queue:" + queueName
				+ ", BindRoutingKey:" + routingKey);
	}
	System.out.println("ReceiveLogsTopic1 Waiting for messages");

	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("ReceiveLogsTopic1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
		}
	};
	channel.basicConsume(queueName, true, consumer);

}
 
開發者ID:pudoj,項目名稱:june.mq,代碼行數:42,代碼來源:ReceiveLogsTopic1.java

示例15: createFromRabbitMqConfig

import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
default Connection createFromRabbitMqConfig(RabbitMqConfig config){
    LOGGER.info("Trying to connect to RabbitMq '{}:{}'.", config.host(), config.port());
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(config.host());
    factory.setPort(config.port());

    if (StringUtils.isNotBlank(config.login())) {
        factory.setUsername(config.login());
        factory.setPassword(config.password());
        LOGGER.debug("Using login : {} [{}]", config.login(), config.password());
    }

    factory.setVirtualHost(config.virtualHost());
    LOGGER.debug("Use virtualhost {}", config.virtualHost());

    try {
        //ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
        return factory.newConnection();
    } catch (IOException e) {
        throw new RuntimeException("Unable to create a consumeConnection to Rabbit " + config.host() + ":" + config.port(), e);
    }
}
 
開發者ID:kodokojo,項目名稱:kodokojo,代碼行數:23,代碼來源:RabbitMqConnectionFactory.java


注:本文中的com.rabbitmq.client.ConnectionFactory.setPort方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。