首页

RabbitMQ入门级示例Demo

java

2020-6-27

package com.rabbitmq.test;

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

public class ProducerTest {

	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("localhost");//因为两个进程在同一个机器上
	    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();


	}

}

资源下载此资源下载价格为1D币(VIP免费),请先
资源文件列表
RabbitMQDemo-C/.classpath , 523
RabbitMQDemo-C/.project , 390
RabbitMQDemo-C/.settings/org.eclipse.jdt.core.prefs , 598
RabbitMQDemo-C/bin/com/rabbitmq/test/ConsumerTests$1.class , 1511
RabbitMQDemo-C/bin/com/rabbitmq/test/ConsumerTests.class , 1797
RabbitMQDemo-C/src/com/rabbitmq/test/ConsumerTests.java , 1035
RabbitMQDemo-P/.classpath , 523
RabbitMQDemo-P/.project , 390
RabbitMQDemo-P/.settings/org.eclipse.jdt.core.prefs , 598
RabbitMQDemo-P/bin/com/rabbitmq/test/ProducerTest.class , 2060
RabbitMQDemo-P/src/com/rabbitmq/test/ProducerTest.java , 960
没有账号? 忘记密码?

社交账号快速登录