/src/test/java/com/rabbitmq/messagepatterns/unicast/UnicastClient.java

http://github.com/rabbitmq/rabbitmq-java-messagepatterns · Java · 105 lines · 88 code · 11 blank · 6 comment · 11 complexity · 25fa2f9f868d5ec0a9d677e02522a899 MD5 · raw file

  1. package com.rabbitmq.messagepatterns.unicast;
  2. import com.rabbitmq.client.ConnectionFactory;
  3. import com.rabbitmq.client.Channel;
  4. import com.rabbitmq.client.Connection;
  5. import java.util.HashSet;
  6. import java.util.Set;
  7. import java.io.IOException;
  8. public class UnicastClient {
  9. public static void main(String[] args) throws Exception {
  10. UnicastClient c = new UnicastClient();
  11. c.run(args[0], args[1], Integer.parseInt(args[2]));
  12. }
  13. Set pending = new HashSet();
  14. int sent; //requests sent
  15. int recv; //requests received
  16. int pend; //pending requests
  17. int disc; //replies discarded
  18. UnicastClient() {
  19. }
  20. MessageSentListener listener = new MessageSentListener() {
  21. public void messageSent(Message msg) {
  22. if (msg.getCorrelationId() == null) {
  23. sent++;
  24. pend++;
  25. }
  26. displayStats();
  27. }
  28. };
  29. void displayStats() {
  30. System.out.printf("\r" +
  31. "sent %d, " +
  32. "recv %d, " +
  33. "pend %d, " +
  34. "disc %d",
  35. sent, recv, pend, disc);
  36. }
  37. void run(final String me, final String you, int sleep) throws Exception {
  38. Connector conn =
  39. Factory.createConnector(new ConnectionBuilder() {
  40. public Connection createConnection() throws IOException {
  41. return new ConnectionFactory().newConnection();
  42. }
  43. });
  44. Messaging m = Factory.createMessaging();
  45. m.setConnector(conn);
  46. m.setIdentity(me);
  47. m.addMessageSentListener(listener);
  48. m.addSenderSetupListener(new ChannelSetupListener() {
  49. public void channelSetup(Channel channel) throws IOException {
  50. //We declare the recipient queue here to avoid
  51. //sending messages into the ether. That's an ok
  52. //thing to do for testing
  53. channel.queueDeclare(you, true, false, false, null); //durable
  54. }
  55. });
  56. m.addReceiverSetupListener(new ChannelSetupListener() {
  57. public void channelSetup(Channel channel) throws IOException {
  58. channel.queueDeclare(me, true, false, false, null); //durable
  59. }
  60. });
  61. m.init();
  62. byte[] body = new byte[0];
  63. for (int i = 0; ; i++) {
  64. //send
  65. Message msg = m.createMessage();
  66. msg.setBody(body);
  67. msg.setTo(you);
  68. msg.getProperties().setDeliveryMode(2);
  69. m.send(msg);
  70. pending.add(msg.getMessageId());
  71. //handle inbound
  72. while (true) {
  73. ReceivedMessage r = m.receiveNoWait();
  74. if (r == null) break;
  75. if (r.getCorrelationId() == null) {
  76. recv++;
  77. displayStats();
  78. m.send(m.createReply(r));
  79. } else {
  80. if (pending.contains(r.getCorrelationId())) {
  81. pending.remove(r.getCorrelationId());
  82. pend--;
  83. } else {
  84. disc++;
  85. }
  86. displayStats();
  87. }
  88. m.ack(r);
  89. }
  90. //Slow down to prevent pending from growing w/o bounds
  91. Thread.sleep(sleep + pend);
  92. }
  93. }
  94. }