@Override
  public void send(
      Destination destination, Message message, int deliveryMode, int priority, long timeToLive)
      throws JMSException {
    message.setJMSDestination(destination);
    com.alibaba.rocketmq.common.message.Message convertedMsg = ((MessageBase) message).convert();
    try {
      if (!started.get()) {
        targetProducer.start();
        started.set(true);
      }

      targetProducer.send(convertedMsg);
    } catch (Exception ex) {
      throw JMSExceptionSupport.create(ex);
    }
  }
 public void start() throws JMSException {
   try {
     if (!started.get()) {
       targetProducer.start();
       started.set(true);
     }
   } catch (MQClientException ex) {
     throw JMSExceptionSupport.create(ex);
   }
 }
Beispiel #3
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();
    }
  }
 @Override
 public void close() throws JMSException {
   try {
     if (started.get()) {
       targetProducer.shutdown();
       targetProducer = null;
       session.removeProducer(this);
       started.set(false);
     }
   } catch (Exception ex) {
     throw JMSExceptionSupport.create(ex);
   }
 }
  /**
   * @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);
  }