Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setMessageModel()
The following examples show how to use
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer#setMessageModel() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BroadcastConsumer.java From netty-chat with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); //set to broadcast mode consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 2
Source File: PushConsumer.java From DDMQ with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 3
Source File: PushConsumer.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 4
Source File: WalletPaymentConsumer.java From order-charge-notify with Apache License 2.0 | 6 votes |
@PostConstruct public void init() { defaultMQPushConsumer = new DefaultMQPushConsumer(MessageProtocolConst.WALLET_PAYMENT_TOPIC.getConsumerGroup()); defaultMQPushConsumer.setNamesrvAddr(nameSrvAddr); // 从头开始消费 defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 消费模式:集群模式 defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); // 注册监听器 defaultMQPushConsumer.registerMessageListener(messageListener); // 订阅所有消息 try { defaultMQPushConsumer.subscribe(MessageProtocolConst.WALLET_PAYMENT_TOPIC.getTopic(), "*"); defaultMQPushConsumer.start(); } catch (MQClientException e) { LOGGER.error("[扣款消息消费者]--WalletPaymentConsumer加载异常!e={}", LogExceptionWapper.getStackTrace(e)); throw new RuntimeException("[扣款消息消费者]--WalletPaymentConsumer加载异常!", e); } LOGGER.info("[扣款消息消费者]--WalletPaymentConsumer加载完成!"); }
Example 5
Source File: NotifySendConsumer.java From order-charge-notify with Apache License 2.0 | 6 votes |
@PostConstruct public void init() { defaultMQPushConsumer = new DefaultMQPushConsumer(MessageProtocolConst.ORDER_RESULT_NOTIFY_TOPIC.getConsumerGroup()); defaultMQPushConsumer.setNamesrvAddr(nameSrvAddr); // 从头开始消费 defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 消费模式:集群模式 defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); // 注册监听器 defaultMQPushConsumer.registerMessageListener(messageListener); // 订阅所有消息 try { defaultMQPushConsumer.subscribe(MessageProtocolConst.ORDER_RESULT_NOTIFY_TOPIC.getTopic(), "*"); defaultMQPushConsumer.start(); } catch (MQClientException e) { LOGGER.error("[订单结果通知消息消费者]--NotifySendConsumer加载异常!e={}", LogExceptionWapper.getStackTrace(e)); throw new RuntimeException("[订单结果通知消息消费者]--NotifySendConsumer加载异常!", e); } LOGGER.info("[订单结果通知消息消费者]--NotifySendConsumer加载完成!"); }
Example 6
Source File: PushConsumer.java From rocketmq-read with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 7
Source File: PushConsumer.java From rocketmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 8
Source File: PushConsumer.java From rocketmq with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); // consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.subscribe("TopicTest", "*"); consumer.setNamesrvAddr("127.0.0.1:9876"); // TODO add by yunai consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 9
Source File: PushConsumer.java From DDMQ with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 10
Source File: BroadcastConsumer.java From spring-boot-starter-samples with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // set to broadcast mode consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 11
Source File: BroadcastConsumer.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void main(String[] args) throws Exception { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name"); consumer.setNamesrvAddr(RocketConfig.HOST); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // set to broadcast mode consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 12
Source File: PushConsumer.java From rocketmq_trans_message with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 13
Source File: PushConsumer.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1"); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.subscribe("TopicTest", "TagA || TagC || TagD"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n"); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Broadcast Consumer Started.%n"); }
Example 14
Source File: BroadcastConsumer2.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { /** TODO: 创建消息消费者对象 */ DefaultMQPushConsumer broadcast_consumer = new DefaultMQPushConsumer("Broadcast_Consumer"); /** TODO: 连接 namerserver */ broadcast_consumer.setNamesrvAddr("127.0.0.1:9876"); /** * TODO: 设置 Consumer 类型 * * <p>默认 CLUSTERING(集群模式,一个 Consumer Group 中的 Consumer 实例平均分摊消费消息); * * <p>BROADCASTING(广播模式,一个消息被多个 Consumer 消费) */ broadcast_consumer.setMessageModel(MessageModel.BROADCASTING); /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */ broadcast_consumer.subscribe("MyTopic", "*"); /** TODO: 注册消息监听 */ broadcast_consumer.registerMessageListener( new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> msgs, ConsumeConcurrentlyContext context) { // TODO: 输出获取的消息 msgs.forEach(o -> System.out.println(new String(o.getBody()))); // TODO: 返回消费结果 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /** TODO: 启动 namerserver 连接 */ broadcast_consumer.start(); System.out.println("BroadcastConsumer2 已启动==========="); }
Example 15
Source File: SecKillChargeOrderConsumer.java From seckill-rocketmq with Apache License 2.0 | 5 votes |
@PostConstruct public void init() { defaultMQPushConsumer = new DefaultMQPushConsumer( MessageProtocolConst.SECKILL_CHARGE_ORDER_TOPIC.getConsumerGroup(), new AclClientRPCHook(new SessionCredentials(aclAccessKey, aclAccessSecret)), // 平均分配队列算法,hash new AllocateMessageQueueAveragely()); defaultMQPushConsumer.setNamesrvAddr(namesrvConfig.nameSrvAddr()); // 从头开始消费 defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 消费模式:集群模式 // 集群:同一条消息 只会被一个消费者节点消费到 // 广播:同一条消息 每个消费者都会消费到 defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING); // 注册监听器 defaultMQPushConsumer.registerMessageListener(messageListener); // 设置每次拉取的消息量,默认为1 defaultMQPushConsumer.setConsumeMessageBatchMaxSize(1); // 订阅所有消息 try { defaultMQPushConsumer.subscribe(MessageProtocolConst.SECKILL_CHARGE_ORDER_TOPIC.getTopic(), "*"); // 启动消费者 defaultMQPushConsumer.start(); } catch (MQClientException e) { LOGGER.error("[秒杀下单消费者]--SecKillChargeOrderConsumer加载异常!e={}", LogExceptionWapper.getStackTrace(e)); throw new RuntimeException("[秒杀下单消费者]--SecKillChargeOrderConsumer加载异常!", e); } LOGGER.info("[秒杀下单消费者]--SecKillChargeOrderConsumer加载完成!"); }
Example 16
Source File: BroadcastConsumer1.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException { /** TODO: 创建消息消费者对象 */ DefaultMQPushConsumer broadcast_consumer = new DefaultMQPushConsumer("Broadcast_Consumer"); /** TODO: 连接 namerserver */ broadcast_consumer.setNamesrvAddr("127.0.0.1:9876"); /** * TODO: 设置 Consumer 类型 * * <p>默认 CLUSTERING(集群模式,一个 Consumer Group 中的 Consumer 实例平均分摊消费消息); * * <p>BROADCASTING(广播模式,一个消息被多个 Consumer 消费) */ broadcast_consumer.setMessageModel(MessageModel.BROADCASTING); /** TODO: 订阅消息主题, 第二个参数表示 指定 订阅的 tag,* 表示全部 */ broadcast_consumer.subscribe("MyTopic", "*"); /** TODO: 注册消息监听 */ broadcast_consumer.registerMessageListener( new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage( List<MessageExt> msgs, ConsumeConcurrentlyContext context) { // TODO: 输出获取的消息 msgs.forEach(o -> System.out.println(new String(o.getBody()))); // TODO: 返回消费结果 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); /** TODO: 启动 namerserver 连接 */ broadcast_consumer.start(); System.out.println("BroadcastConsumer1 已启动==========="); }
Example 17
Source File: RockMqTestCase.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Test public void testTopic() throws Exception { int size = 5; DefaultMQPushConsumer[] consumers = new DefaultMQPushConsumer[size]; for (int index = 0; index < size; index++) { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("topic"); consumer.setInstanceName("topicConsumer" + index); consumer.setNamesrvAddr("localhost:9876"); consumer.setMessageModel(MessageModel.BROADCASTING); consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setConsumeMessageBatchMaxSize(1000); consumer.subscribe("topic-message", "*"); consumer.registerMessageListener(listener); consumer.start(); consumers[index] = consumer; } DefaultMQProducer producer = new DefaultMQProducer("topic"); producer.setNamesrvAddr("localhost:9876"); producer.start(); for (int index = 0; index < 10; index++) { Message message = new Message("topic-message", "tag", ("RocketMQ 广播模式 " + index).getBytes(StringUtility.CHARSET)); producer.send(message); } semaphore.acquire(50); producer.shutdown(); for (int index = 0; index < size; index++) { consumers[index].shutdown(); } }
Example 18
Source File: AbstractRocketMqConsumer.java From spring-boot-rocketmq-starter with Apache License 2.0 | 5 votes |
private void initRocketMQPushConsumer() throws MQClientException { Assert.notNull(getConsumerGroup(), "Property 'consumerGroup' is required"); Assert.notEmpty(subscribeTopicTags(), "subscribeTopicTags method can't be empty"); consumer = new DefaultMQPushConsumer(getConsumerGroup()); if (consumeThreadMax != null) { consumer.setConsumeThreadMax(consumeThreadMax); } if (consumeThreadMax != null && consumeThreadMax < consumer.getConsumeThreadMin()) { consumer.setConsumeThreadMin(consumeThreadMax); } consumer.setConsumeFromWhere(consumeFromWhere); consumer.setMessageModel(messageModel); switch (consumeMode) { case Orderly: consumer.setMessageListener(new DefaultMessageListenerOrderly()); break; case CONCURRENTLY: consumer.setMessageListener(new DefaultMessageListenerConcurrently()); break; default: throw new IllegalArgumentException("Property 'consumeMode' was wrong."); } }
Example 19
Source File: RocketEventChannel.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void registerMonitor(Set<Class> types, EventMonitor monitor) { try { for (Class type : types) { EventManager manager = managers.get(type); if (manager == null) { manager = new EventManager(); managers.put(type, manager); // TODO 需要防止路径冲突 String address = name + StringUtility.DOT + type.getName(); address = address.replace(StringUtility.DOT, StringUtility.DASH); DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(address); consumer.setInstanceName(name); consumer.setNamesrvAddr(connections); consumer.setConsumeMessageBatchMaxSize(1000); consumer.subscribe(address, "*"); switch (mode) { case QUEUE: { consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.setMessageModel(MessageModel.CLUSTERING); break; } case TOPIC: { consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); consumer.setMessageModel(MessageModel.BROADCASTING); break; } } EventHandler handler = new EventHandler(type, manager); consumer.registerMessageListener(handler); consumer.start(); consumers.put(type, consumer); } manager.attachMonitor(monitor); } } catch (Exception exception) { throw new RuntimeException(exception); } }
Example 20
Source File: DefaultRocketMQListenerContainer.java From spring-boot-starter-rocketmq with Apache License 2.0 | 4 votes |
private void initRocketMQPushConsumer() throws MQClientException { Assert.notNull(rocketMQListener, "Property 'rocketMQListener' is required"); Assert.notNull(consumerGroup, "Property 'consumerGroup' is required"); Assert.notNull(nameServer, "Property 'nameServer' is required"); Assert.notNull(topic, "Property 'topic' is required"); consumer = new DefaultMQPushConsumer(consumerGroup); consumer.setNamesrvAddr(nameServer); consumer.setConsumeThreadMax(consumeThreadMax); if (consumeThreadMax < consumer.getConsumeThreadMin()) { consumer.setConsumeThreadMin(consumeThreadMax); } consumer.setPullThresholdForTopic(pullThresholdForTopic); consumer.setPullThresholdSizeForTopic(pullThresholdSizeForTopic); consumer.setMessageModel(messageModel); switch (selectorType) { case TAG: consumer.subscribe(topic, selectorExpress); break; case SQL92: consumer.subscribe(topic, MessageSelector.bySql(selectorExpress)); break; default: throw new IllegalArgumentException("Property 'selectorType' was wrong."); } switch (consumeMode) { case ORDERLY: consumer.setMessageListener(new DefaultMessageListenerOrderly()); break; case CONCURRENTLY: consumer.setMessageListener(new DefaultMessageListenerConcurrently()); break; default: throw new IllegalArgumentException("Property 'consumeMode' was wrong."); } // provide an entryway to custom setting RocketMQ consumer if (rocketMQListener instanceof RocketMQPushConsumerLifecycleListener) { ((RocketMQPushConsumerLifecycleListener) rocketMQListener).prepareStart(consumer); } }