Search in sources :

Example 1 with MQProducer

use of org.apache.rocketmq.client.producer.MQProducer in project rocketmq-externals by apache.

the class JmsBaseMessageProducer method send.

/**
 * Send the message to the defined Destination success---return normally. Exception---throw out JMSException.
 *
 * @param destination see <CODE>Destination</CODE>
 * @param message the message to be sent.
 * @throws javax.jms.JMSException
 */
@Override
public void send(Destination destination, javax.jms.Message message) throws JMSException {
    JmsBaseMessage jmsMsg = (JmsBaseMessage) message;
    initJMSHeaders(jmsMsg, destination);
    try {
        if (context == null) {
            throw new IllegalStateException("Context should be inited");
        }
        org.apache.rocketmq.common.message.Message rocketmqMsg = MessageConverter.convert2RMQMessage(jmsMsg);
        MQProducer producer = producerMap.get(context.getProducerId());
        if (producer == null) {
            throw new Exception("producer is null ");
        }
        SendResult sendResult = producer.send(rocketmqMsg);
        if (sendResult != null && sendResult.getSendStatus() == SendStatus.SEND_OK) {
            jmsMsg.setHeader(JmsBaseConstant.JMS_MESSAGE_ID, "ID:" + sendResult.getMsgId());
        } else {
            throw new Exception("SendResult is " + (sendResult == null ? "null" : sendResult.toString()));
        }
    } catch (Exception e) {
        logger.error("Send rocketmq message failure !", e);
        // if fail to send the message, throw out JMSException
        JMSException jmsException = new JMSException("Send rocketmq message failure!");
        jmsException.setLinkedException(e);
        throw jmsException;
    }
}
Also used : MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with MQProducer

use of org.apache.rocketmq.client.producer.MQProducer in project rocketmq-externals by apache.

the class JmsTestUtil method getMQProducer.

public static MQProducer getMQProducer(String producerId) throws Exception {
    Assert.assertNotNull(producerId);
    Field field = JmsBaseMessageProducer.class.getDeclaredField("producerMap");
    field.setAccessible(true);
    ConcurrentMap<String, MQProducer> producerMap = (ConcurrentMap<String, MQProducer>) field.get(null);
    return producerMap.get(producerId);
}
Also used : Field(java.lang.reflect.Field) MQProducer(org.apache.rocketmq.client.producer.MQProducer) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 3 with MQProducer

use of org.apache.rocketmq.client.producer.MQProducer in project spring-boot-starter-samples by vindell.

the class OrderedProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    MQProducer producer = new DefaultMQProducer("example_group_name");
    // Launch the instance.
    producer.start();
    String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
    for (int i = 0; i < 100; i++) {
        int orderId = i % 10;
        // Create a message instance, specifying topic, tag and message body.
        Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
        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.printf("%s%n", sendResult);
    }
    // server shutdown
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SendResult(org.apache.rocketmq.client.producer.SendResult)

Aggregations

MQProducer (org.apache.rocketmq.client.producer.MQProducer)3 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 SendResult (org.apache.rocketmq.client.producer.SendResult)2 Field (java.lang.reflect.Field)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 JMSException (javax.jms.JMSException)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)1 Message (org.apache.rocketmq.common.message.Message)1 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)1 JmsBaseMessage (org.apache.rocketmq.jms.domain.message.JmsBaseMessage)1