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;
}
}
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);
}
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();
}
Aggregations