Exemplo n.º 1
0
  public static void main(String[] args) {
    try {
      MQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");

      producer.start();

      String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};

      for (int i = 0; i < 100; i++) {
        // 订单ID相同的消息要有序
        int orderId = i % 10;
        Message msg =
            new Message(
                "TopicTest", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes());

        SendResult sendResult =
            producer.send(
                msg,
                new MessageQueueSelector() {
                  @Override
                  public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                  }
                },
                orderId);

        System.out.println(sendResult);
      }

      producer.shutdown();
    } catch (MQClientException e) {
      e.printStackTrace();
    } catch (RemotingException e) {
      e.printStackTrace();
    } catch (MQBrokerException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
  /**
   * @param args
   * @throws MQClientException
   */
  public static void main(String[] args) throws MQClientException {

    MQProducer oneWayOrderProducer = new DefaultMQProducer("example.producer");

    oneWayOrderProducer.start();

    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    MessageQueueSelector selector =
        new MessageQueueSelector() {
          @Override
          public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
            Integer id = (Integer) arg;
            int index = id % mqs.size();
            return mqs.get(index);
          }
        };
    for (int i = 0; i < 10; i++) {
      try {
        int orderId = i % 10;
        Message msg =
            new Message(
                "TopicTest",
                tags[i % tags.length],
                "KEY" + i,
                ("Hello RocketMQ from OneWay" + i).getBytes());

        oneWayOrderProducer.sendOneway(msg, selector, orderId);
      } catch (RemotingException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (MQClientException e1) {
        e1.printStackTrace();
      }
    }
    oneWayOrderProducer.shutdown();
    System.exit(0);
  }
Exemplo n.º 3
0
  public static void main(String[] args) throws MQClientException, InterruptedException {

    TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    // 事务回查最小并发数
    producer.setCheckThreadPoolMinSize(2);
    // 事务回查最大并发数
    producer.setCheckThreadPoolMaxSize(2);
    // 队列数
    producer.setCheckRequestHoldMax(2000);

    producer.setNamesrvAddr("127.0.0.1:9876");
    producer.setTransactionCheckListener(transactionCheckListener);
    producer.start();

    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    TransactionExecuterImpl tranExecuter = new TransactionExecuterImpl();
    for (int i = 0; i < 100; i++) {
      try {
        Message msg =
            new Message(
                "TopicTest", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes());
        SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
        System.out.println(sendResult);

        Thread.sleep(10);
      } catch (MQClientException e) {
        e.printStackTrace();
      }
    }

    for (int i = 0; i < 100000; i++) {
      Thread.sleep(1000);
    }

    producer.shutdown();
  }