@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);
    }
  }
示例#2
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();
    }
  }