本文整理匯總了Java中com.rabbitmq.client.Channel類的典型用法代碼示例。如果您正苦於以下問題:Java Channel類的具體用法?Java Channel怎麽用?Java Channel使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Channel類屬於com.rabbitmq.client包,在下文中一共展示了Channel類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.rabbitmq.client.Channel; //導入依賴的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: close
import com.rabbitmq.client.Channel; //導入依賴的package包/類
private void close(Connection connection, Channel channel) {
try {
if (channel != null && channel.isOpen()) {
if (this.consumerTag != null) {
channel.basicCancel(this.consumerTag);
this.consumerTag = null;
}
log.info("Closing RabbitMQ Channel - " + this.serverIP);
channel.close();
this.channel = null;
}
if (connection != null && connection.isOpen()) {
log.info("Closing RabbitMQ Connection - " + this.serverIP);
connection.close(CLOSE_CONNECTION_TIMEOUT);
this.connection = null;
}
} catch (Exception e) {
log.error("Failed to close RabbitMQ connections " + this.serverIP, e);
}
}
示例3: 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();
}
示例4: main
import com.rabbitmq.client.Channel; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
Channel channel = AMQPCommon.connect();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicQos(1);
channel.basicConsume("trade.eq.q", false, consumer);
int numMsgs = args.length > 0 ? new Integer(args[0]).intValue() : 1;
for (int i=0; i<numMsgs; i++) {
QueueingConsumer.Delivery msg = consumer.nextDelivery();
System.out.println("message received: " + new String(msg.getBody()));
Thread.sleep(1000);
channel.basicAck(msg.getEnvelope().getDeliveryTag(), false);
}
AMQPCommon.close(channel);
}
示例5: main
import com.rabbitmq.client.Channel; //導入依賴的package包/類
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUri("amqp://guest:[email protected]");
factory.setConnectionTimeout(300000);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("my-queue", true, false, false, null);
int count = 0;
while (count < 5000) {
String message = "Message number " + count;
channel.basicPublish("", "my-queue", null, message.getBytes());
count++;
System.out.println("Published message: " + message);
Thread.sleep(5000);
}
}
示例6: onMessage
import com.rabbitmq.client.Channel; //導入依賴的package包/類
/**
* 普通消息監聽
*
* @param message 消息實體
* @param channel channel 就是當前的會話通道
* @throws Exception 備注: 手動ack就是在當前channel裏麵調用basicAsk的方法,並傳入當前消息的tagId就可以了。
*/
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
logger.debug("deliveryTag= " + deliveryTag);
try {
logger.info("------消費者處理消息------");
logger.info("receive message" + message.getMessageProperties().getAppId());
logger.info("receive channel" + channel.getChannelNumber() + "----");
// 獲取消息
if (null != message.getBody()) {
EventMessage eventMessage = (EventMessage) ObjectAndByteCovertUtil.ByteToObject(message.getBody());
if (null != eventMessage) {
System.out.println(Thread.currentThread().getName() + ":" + TimeUtils.getSysTime("yyyy-MM-dd HH:mm:ss") + ":[下遊應用- 消費普通消息]:" + message.getMessageProperties());
// TODO 業務處理
}
}
// 消息的標識,false隻確認當前一個消息收到,true確認所有consumer獲得的消息(成功消費,消息從隊列中刪除 )
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
logger.warn("message consume failed: " + e.getMessage());
// ack返回false,requeue-true並重新回到隊列
channel.basicNack(deliveryTag, false, true);
}
}
示例7: 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();
}
示例8: main
import com.rabbitmq.client.Channel; //導入依賴的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();
}
示例9: 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();
}
示例10: main
import com.rabbitmq.client.Channel; //導入依賴的package包/類
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
// factory.setHost("");
factory.setUri("amqp://alpha.netkiller.cn");
factory.setUsername("admin");
// factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
示例11: 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();
}
示例12: 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.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
String message = getMessage(argv);
channel.basicPublish("", TASK_QUEUE_NAME,
MessageProperties.PERSISTENT_TEXT_PLAIN,
message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
示例13: readMessage
import com.rabbitmq.client.Channel; //導入依賴的package包/類
private byte[] readMessage() throws Exception {
final CountDownLatch countDown = new CountDownLatch(1);
final AtomicReference<byte[]> result = new AtomicReference<>();
Channel channel = sender.get().createChannel();
try {
channel.basicConsume(sender.queue(), true, new DefaultConsumer(channel) {
@Override public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
result.set(body);
countDown.countDown();
}
});
countDown.await(5, TimeUnit.SECONDS);
} finally {
channel.close();
}
return result.get();
}
示例14: execute
import com.rabbitmq.client.Channel; //導入依賴的package包/類
/**
* Do something with a pooled channel (similar to Spring JDBC TransactionTemplate#execute)
*/
private <T> T execute(ChannelCallback<T> callback) throws Exception {
Channel channel;
try {
channel = channelPool.borrowObject();
} catch (IllegalStateException e) {
// Since this method is not synchronized its possible the
// channelPool has been cleared by another thread
checkConnectionAndChannelPool();
channel = channelPool.borrowObject();
}
if (!channel.isOpen()) {
log.warn("Got a closed channel from the pool");
// Reconnect if another thread hasn't yet
checkConnectionAndChannelPool();
channel = channelPool.borrowObject();
}
try {
return callback.doWithChannel(channel);
} finally {
channelPool.returnObject(channel);
}
}
示例15: onMessage
import com.rabbitmq.client.Channel; //導入依賴的package包/類
/**
* 延遲消息監聽並處理
*
* @param message 消息實體
* @param channel channel 就是當前的會話通道
* @throws Exception 備注: 手動ack就是在當前channel裏麵調用basicAsk的方法,並傳入當前消息的tagId就可以了。
*/
@Override
public void onMessage(Message message, Channel channel) throws Exception {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
logger.debug("deliveryTag= " + deliveryTag);
try {
logger.info("[延時消息]" + message.getMessageProperties());
// 獲取消息
if (null != message.getBody()) {
EventMessage eventMessage = (EventMessage) ObjectAndByteCovertUtil.ByteToObject(message.getBody());
if (null != eventMessage) {
System.out.println(Thread.currentThread().getName() + ":" + TimeUtils.getSysTime("yyyy-MM-dd HH:mm:ss") + ":[下遊應用-消費延時消息]:" + eventMessage.getObject().toString());
// TODO 業務處理
}
}
// 手動確認 - false隻確認當前一個消息收到,true確認所有consumer獲得的消息
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
logger.warn("message consume failed: " + e.getMessage());
// ack返回false,requeue-true並重新回到隊列
channel.basicNack(deliveryTag, false, true);
}
}