本文整理匯總了Java中com.rabbitmq.client.Channel.exchangeDeclare方法的典型用法代碼示例。如果您正苦於以下問題:Java Channel.exchangeDeclare方法的具體用法?Java Channel.exchangeDeclare怎麽用?Java Channel.exchangeDeclare使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.rabbitmq.client.Channel
的用法示例。
在下文中一共展示了Channel.exchangeDeclare方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
示例2: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String msg = getMessage(argv);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy @ HH:mm:ss");
String sDate = sdf.format(date);
String finalMsg = sDate + ": " + msg;
channel.basicPublish(EXCHANGE_NAME, "", null, finalMsg.getBytes("UTF-8"));
System.out.println("Emmited message: " + finalMsg);
channel.close();
conn.close();
}
示例3: main
import com.rabbitmq.client.Channel; //導入方法依賴的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();
}
示例4: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
String severity = getSeverity(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
channel.close();
connection.close();
}
示例5: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:37:37
* @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, "fanout");// fanout表示分發,所有的消費者得到同樣的隊列信息
// 分發信息
for (int i = 0; i < 5; i++) {
String message = "Hello World" + i;
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println("EmitLog Sent '" + message + "'");
}
channel.close();
connection.close();
}
示例6: run
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
@Override
public void run(AppConfiguration configuration, Environment environment) throws Exception {
SessionFactory sessionFactory = hibernate.getSessionFactory();
MemoDAO memoDAO = new MemoDAO(sessionFactory);
QueueConfig queueConfig = configuration.getQueueConfig();
ConnectionFactory connectionFactory = QueueHelper.getQueue(queueConfig);
Channel channel = connectionFactory.newConnection().createChannel();
channel.exchangeDeclare(queueConfig.getExchangeName(), "direct", true);
channel.queueDeclare(queueConfig.getQueueName(), true, false, false, null);
channel.queueBind(queueConfig.getQueueName(), queueConfig.getExchangeName(), queueConfig.getRoutingKey());
channel.basicConsume(queueConfig.getQueueName(), false, "myConsumerTag", new MemoWorker(memoDAO, channel, sessionFactory));
}
示例7: main
import com.rabbitmq.client.Channel; //導入方法依賴的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();
}
示例8: main
import com.rabbitmq.client.Channel; //導入方法依賴的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);
}
示例9: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:53:18
* @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 routingKey : routingKeys1) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, routingKey);
System.out.println("ReceiveLogsDirect1 exchange:" + EXCHANGE_NAME_ROUTING + "," + " queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsDirect1 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("ReceiveLogsDirect1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例10: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:40:52
* @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, "fanout");
// 產生一個隨機的隊列名稱
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");// 對隊列進行綁定
System.out.println("ReceiveLogs1 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("ReceiveLogs1 Received '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);// 隊列會自動刪除
}
示例11: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午3:03:24
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
connection = factory.newConnection();
channel = connection.createChannel();
// 聲明一個匹配模式的交換機
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
// 待發送的消息
String[] routingKeys = new String[] { "quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox",
"lazy.brown.fox", "quick.brown.fox", "quick.orange.male.rabbit", "lazy.orange.male.rabbit" };
// 發送消息
for (String severity : routingKeys) {
String message = "From " + severity + " routingKey' s message!";
channel.basicPublish(EXCHANGE_NAME_TOPIC, severity, null, message.getBytes());
System.out.println("TopicSend Sent '" + severity + "':'" + message + "'");
}
} catch (Exception e) {
e.printStackTrace();
if (connection != null) {
channel.close();
connection.close();
}
} finally {
if (connection != null) {
channel.close();
connection.close();
}
}
}
示例12: main
import com.rabbitmq.client.Channel; //導入方法依賴的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);
}
示例13: main
import com.rabbitmq.client.Channel; //導入方法依賴的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);
}
示例14: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] argv) throws java.io.IOException, TimeoutException {
Rabbit rabbit = new Rabbit("localhost");
Channel channel = rabbit.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
sendMessage(channel, EXCHANGE_NAME, "Widget", "Hello EIP Widget!");
sendMessage(channel, EXCHANGE_NAME, "Gadget", "Hello EIP Gadget!");
channel.close();
rabbit.close();
}
示例15: main
import com.rabbitmq.client.Channel; //導入方法依賴的package包/類
public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
String routingKey = getRouting(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}