重庆幸运农场中奖金额|重庆幸运农场官网
MyException - 我的異常網
當前位置:我的異常網» 軟件架構設計 » (java)簡略實現原生RabbitMQ中的廣播訂閱(fanout

(java)簡略實現原生RabbitMQ中的廣播訂閱(fanout)模式

www.h0f1.com  網友分享于:2018-06-06  瀏覽:0次
(java)簡單實現原生RabbitMQ中的廣播訂閱(fanout)模式

fanout 發送端

?

  1. import?com.rabbitmq.client.Channel;??
  2. import?com.rabbitmq.client.Connection;??
  3. import?com.rabbitmq.client.ConnectionFactory;??
  4. ??
  5. public?class?MyFanoutSender?{??
  6. ??????
  7. ?????
  8. ????private?static?final?String?MESSAGE?=?"my?name?is";??
  9. ??????
  10. ????public?static?void?main(String[]?args){??
  11. ????????Connection?conn?=?null;??
  12. ????????Channel?channel?=?null;??
  13. ????????try?{??
  14. ????????????//初始化連接,主機,端口,用戶名,密碼可以自己定義??
  15. ????????????ConnectionFactory?factory?=?new?ConnectionFactory();??
  16. ????????????factory.setHost(HOST_PARAMETER.LOCAL_HOST);??
  17. ????????????factory.setPort(HOST_PARAMETER.LOCAL_PORT);??
  18. ????????????factory.setUsername(HOST_PARAMETER.LOCAL_USER_NAME);??
  19. ????????????factory.setPassword(HOST_PARAMETER.LOCAL_PASSWORD);??
  20. ????????????//創建連接??
  21. ????????????conn?=?factory.newConnection();??
  22. ????????????//創建通道??
  23. ????????????channel?=?conn.createChannel();??
  24. ????????????//定義為fanout類型的交換機??
  25. ????????????channel.exchangeDeclare(HOST_PARAMETER.EXCHANGE_NAME,?"fanout");??
  26. ????????????//發送,指定routingkey為""??
  27. ????????????channel.basicPublish(HOST_PARAMETER.EXCHANGE_NAME,?"",?null,?MESSAGE.getBytes());??
  28. ????????????System.out.println("I?send?a?fanout?massage!");??
  29. ????????}?catch?(Exception?e)?{??
  30. ????????????e.printStackTrace();??
  31. ????????}?finally{??
  32. ????????????try?{??
  33. ????????????????if(channel?!=?null){??
  34. ????????????????????channel.close();??
  35. ????????????????}??
  36. ????????????????if(conn?!=?null){??
  37. ????????????????????conn.close();??
  38. ????????????????}??
  39. ????????????}?catch?(Exception?e)?{??
  40. ????????????????e.printStackTrace();??
  41. ????????????}??
  42. ????????}??
  43. ????}??
  44. }??


fanout訂閱者(接收端),可以自己多定義幾個,才能看出效果

?

  1. import?java.io.IOException;??
  2. ??
  3. import?com.rabbitmq.client.AMQP.BasicProperties;??
  4. import?com.rabbitmq.client.Channel;??
  5. import?com.rabbitmq.client.Connection;??
  6. import?com.rabbitmq.client.ConnectionFactory;??
  7. import?com.rabbitmq.client.Consumer;??
  8. import?com.rabbitmq.client.DefaultConsumer;??
  9. import?com.rabbitmq.client.Envelope;??
  10. ??
  11. public?class?MyFirstFanoutReceiver?{??
  12. ??????
  13. ??public?static?void?main(String[]?args){??
  14. ??????Connection?conn?=?null;??
  15. ??????Channel?channel?=?null;??
  16. ??????try?{??
  17. ????????//初始化連接??
  18. ????????ConnectionFactory?factory?=?new?ConnectionFactory();??
  19. ????????factory.setHost(HOST_PARAMETER.LOCAL_HOST);??
  20. ????????factory.setPort(HOST_PARAMETER.LOCAL_PORT);??
  21. ????????factory.setUsername(HOST_PARAMETER.LOCAL_USER_NAME);??
  22. ????????factory.setPassword(HOST_PARAMETER.LOCAL_PASSWORD);??
  23. ????????//創建連接??
  24. ????????conn?=?factory.newConnection();??
  25. ????????//創建通道??
  26. ????????channel?=?conn.createChannel();??
  27. ????????//聲明交換機類型??
  28. ????????channel.exchangeDeclare(HOST_PARAMETER.EXCHANGE_NAME,?"fanout");??
  29. ????????//聲明默認的隊列??
  30. ????????String?queue?=?channel.queueDeclare().getQueue();??
  31. ????????//將隊列與交換機綁定,最后一個參數為routingKey,與發送者指定的一樣""??
  32. ????????channel.queueBind(queue,?HOST_PARAMETER.EXCHANGE_NAME,?"");??
  33. ????????//消費者??
  34. ????????Consumer?consumer?=?new?DefaultConsumer(channel){??
  35. [email protected]??
  36. ????????????public?void?handleDelivery(String?consumerTag,?Envelope?envelope,?BasicProperties?properties,?byte[]?body)??
  37. ????????????????????throws?IOException?{??
  38. ????????????????System.out.println(new?String(body,"utf-8")+"?Tom");??
  39. ????????????}??
  40. ??????????????
  41. ????????};??
  42. ????????channel.basicConsume(queue,?true,?consumer);??
  43. ????????System.out.println("i?am?the?first?fanout?receiver!");??
  44. ??????}catch?(Exception?e)?{??
  45. ????????????e.printStackTrace();??
  46. ??????}???
  47. ??}??
  48. }??

文章評論

軟件開發程序錯誤異常ExceptionCopyright © 2009-2015 MyException 版權所有
重庆幸运农场中奖金额