Producer.java 747 B

123456789101112131415161718192021222324
  1. package com.sf.workqueue;
  2. import com.rabbitmq.client.Channel;
  3. import com.sf.util.RabbitMqUtils;
  4. import java.util.Scanner;
  5. public class Producer {
  6. private static String queueName = "hello";
  7. public static void main(String[] args) throws Exception {
  8. Channel channel = RabbitMqUtils.getChannel();
  9. // 声明队列
  10. channel.queueDeclare(queueName, false, false, false, null);
  11. System.out.println("输入消息:");
  12. Scanner scanner = new Scanner(System.in);
  13. while (scanner.hasNext()) {
  14. String message = scanner.next();
  15. channel.basicPublish("", queueName, null, message.getBytes());
  16. System.out.println("Producer send message : " + message);
  17. }
  18. }
  19. }