io.openmessaging.consumer.MessageListener Java Examples
The following examples show how to use
io.openmessaging.consumer.MessageListener.
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: PushConsumerImplTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Test public void testConsumeMessage() { final byte[] testBody = new byte[] {'a', 'b'}; MessageExt consumedMsg = new MessageExt(); consumedMsg.setMsgId("NewMsgId"); consumedMsg.setBody(testBody); consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); consumedMsg.setTopic("HELLO_QUEUE"); consumer.attachQueue("HELLO_QUEUE", new MessageListener() { @Override public void onReceived(Message message, Context context) { assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId"); assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody); context.ack(); } }); ((MessageListenerConcurrently) rocketmqPushConsumer .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null); }
Example #2
Source File: PushConsumerImplTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Test public void testConsumeMessage() { final byte[] testBody = new byte[] {'a', 'b'}; MessageExt consumedMsg = new MessageExt(); consumedMsg.setMsgId("NewMsgId"); consumedMsg.setBody(testBody); consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); consumedMsg.setTopic("HELLO_QUEUE"); consumer.attachQueue("HELLO_QUEUE", new MessageListener() { @Override public void onReceived(Message message, Context context) { assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId"); assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody); context.ack(); } }); ((MessageListenerConcurrently) rocketmqPushConsumer .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null); }
Example #3
Source File: ConsumerContainer.java From joyqueue with Apache License 2.0 | 6 votes |
@Override public void afterPropertiesSet() { Consumer consumer = accessPointContainer.getAccessPoint().createConsumer(); consumer.start(); for (ConsumerInterceptor interceptor : accessPointContainer.getConsumerInterceptors()) { consumer.addInterceptor(interceptor); } Object messageListener = applicationContext.getBean(messageListenerId); if (messageListener instanceof MessageListener) { consumer.bindQueue(queueName, (MessageListener) messageListener); } else if (messageListener instanceof BatchMessageListener) { consumer.bindQueue(queueName, (BatchMessageListener) messageListener); } else { throw new IllegalArgumentException("listener type error, need MessageListener or BatchMessageListener"); } this.consumer = consumer; }
Example #4
Source File: PushConsumerImplTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Test public void testConsumeMessage() { final byte[] testBody = new byte[] {'a', 'b'}; MessageExt consumedMsg = new MessageExt(); consumedMsg.setMsgId("NewMsgId"); consumedMsg.setBody(testBody); consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); consumedMsg.setTopic("HELLO_QUEUE"); consumer.attachQueue("HELLO_QUEUE", new MessageListener() { @Override public void onReceived(Message message, Context context) { assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId"); assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody); context.ack(); } }); ((MessageListenerConcurrently) rocketmqPushConsumer .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null); }
Example #5
Source File: PushConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
@Override public PushConsumer attachQueue(final String queueName, final MessageListener listener) { this.subscribeTable.put(queueName, listener); try { this.rocketmqPushConsumer.subscribe(queueName, "*"); } catch (MQClientException e) { throw new OMSRuntimeException("-1", String.format("RocketMQ push consumer can't attach to %s.", queueName)); } return this; }
Example #6
Source File: PushConsumerImpl.java From rocketmq with Apache License 2.0 | 5 votes |
@Override public PushConsumer attachQueue(final String queueName, final MessageListener listener) { this.subscribeTable.put(queueName, listener); try { this.rocketmqPushConsumer.subscribe(queueName, "*"); } catch (MQClientException e) { throw new OMSRuntimeException("-1", String.format("RocketMQ push consumer can't attach to %s.", queueName)); } return this; }
Example #7
Source File: SimplePushConsumer.java From rocketmq with Apache License 2.0 | 5 votes |
public static void main(String[] args) { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://localhost:9876/default:default"); final PushConsumer consumer = messagingAccessPoint. createPushConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "OMS_CONSUMER")); messagingAccessPoint.startup(); System.out.printf("MessagingAccessPoint startup OK%n"); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { consumer.shutdown(); messagingAccessPoint.shutdown(); } })); consumer.attachQueue("OMS_HELLO_TOPIC", new MessageListener() { @Override public void onReceived(Message message, Context context) { System.out.printf("Received one message: %s%n", message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)); context.ack(); } }); consumer.startup(); System.out.printf("Consumer startup OK%n"); }
Example #8
Source File: PushConsumerApp.java From openmessaging-java with Apache License 2.0 | 5 votes |
public static void main(String[] args) { //Load and start the vendor implementation from a specific OMS driver URL. final MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:rocketmq://localhost:10911/us-east"); //Fetch a ResourceManager to create Queue resource. ResourceManager resourceManager = messagingAccessPoint.resourceManager(); resourceManager.createNamespace("NS://XXXX"); final PushConsumer consumer = messagingAccessPoint.createPushConsumer(); consumer.start(); //Register a shutdown hook to close the opened endpoints. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { consumer.stop(); } })); //Consume messages from a simple queue. String simpleQueue = "NS://HELLO_QUEUE"; resourceManager.createQueue(simpleQueue); //This queue doesn't has a source queue, so only the message delivered to the queue directly can //be consumed by this consumer. consumer.bindQueue(Arrays.asList(simpleQueue), new MessageListener() { @Override public void onReceived(Message message, Context context) { System.out.println("Received one message: " + message); context.ack(); } }); consumer.unbindQueue(Arrays.asList(simpleQueue)); consumer.stop(); }
Example #9
Source File: ConsumerRegistrar.java From joyqueue with Apache License 2.0 | 5 votes |
protected String getMethodMessageListener(OMSMessageListener messageListenerAnnotation, String beanId, Method method) { Class<?>[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); boolean isListener = (parameterTypes.length == 2 && parameterTypes[0].equals(Message.class) && parameterTypes[1].equals(MessageListener.Context.class)); boolean isBatchListener = (parameterTypes.length == 2 && genericParameterTypes[0].getTypeName().equals(String.format("java.util.List<%s>", Message.class.getName())) && parameterTypes[1].equals(BatchMessageListener.Context.class)); if (!isListener && !isBatchListener) { throw new IllegalArgumentException("listener parameters error, need MessageListener.onReceived or BatchMessageListener.onReceived"); } BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory; String id = String.format(CONSUMER_CONTAINER_ID, OMSSpringConsts.BEAN_ID_PREFIX, SEQUENCE.getAndIncrement()); BeanDefinition consumerBeanDefinition = null; if (isListener) { consumerBeanDefinition = BeanDefinitionBuilder.rootBeanDefinition(MessageListenerReflectAdapter.class) .addConstructorArgValue(beanId) .addConstructorArgValue(method) .getBeanDefinition(); } else { consumerBeanDefinition = BeanDefinitionBuilder.rootBeanDefinition(BatchMessageListenerReflectAdapter.class) .addConstructorArgValue(beanId) .addConstructorArgValue(method) .getBeanDefinition(); } beanDefinitionRegistry.registerBeanDefinition(id, consumerBeanDefinition); return id; }
Example #10
Source File: ConsumerRegistrar.java From joyqueue with Apache License 2.0 | 5 votes |
protected void checkMessageListener(OMSMessageListener messageListenerAnnotation, String bean) { BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory; BeanDefinition listenerBeanDefinition = beanDefinitionRegistry.getBeanDefinition(bean); Class<?> listenerClass; try { listenerClass = Class.forName(listenerBeanDefinition.getBeanClassName()); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(String.format("listener class not found, className: %s", listenerBeanDefinition.getBeanClassName()), e); } if (!MessageListener.class.isAssignableFrom(listenerClass) && !BatchMessageListener.class.isAssignableFrom(listenerClass)) { throw new IllegalArgumentException(String.format("%s type error, need MessageListener or BatchMessageListener", listenerClass)); } }
Example #11
Source File: SimpleConsumer.java From joyqueue with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { final String app = "test_app"; final String token = "some token"; final String dataCenter = "default"; final String brokerAddr = "127.0.0.1:50088"; final String topic = "test_topic_0"; // oms:joyqueue://[email protected]:50088/default final String url = "oms:joyqueue://" + app + "@" + brokerAddr + "/" + dataCenter; KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, token); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(url, keyValue); // 创建consumer实例 Consumer consumer = messagingAccessPoint.createConsumer(); // 绑定需要消费的topic和对应的listener consumer.bindQueue(topic, new MessageListener() { @Override public void onReceived(Message message, Context context) { System.out.println(String.format("onReceived, message: %s", message)); // 确认消息消费成功,如果没有确认或抛出异常会进入重试队列 context.ack(); } }); consumer.start(); System.in.read(); }
Example #12
Source File: ConsumerImpl.java From joyqueue with Apache License 2.0 | 5 votes |
@Override public void bindQueue(String queueName, MessageListener listener) { try { Preconditions.checkArgument(StringUtils.isNotBlank(queueName), "queueName can not be null"); Preconditions.checkArgument(listener != null, "listener can not be null"); messageConsumer.subscribe(queueName, new MessageListenerAdapter(listener)); } catch (Throwable cause) { throw handleConsumeException(cause); } }
Example #13
Source File: ConsoleConsumer.java From joyqueue with Apache License 2.0 | 5 votes |
protected static void run(Consumer consumer, ConsoleConsumerConfig config) { consumer.bindQueue(config.getTopic(), new MessageListener() { @Override public void onReceived(Message message, Context context) { Header header = message.header(); ExtensionHeader extensionHeader = message.extensionHeader().get(); logger.info("Message{topic: {}, partition: {}, index: {}, txId: {}, key: {}, body: {}}", header.getDestination(), extensionHeader.getPartiton(), extensionHeader.getOffset(), extensionHeader.getTransactionId(), extensionHeader.getMessageKey(), new String(message.getData())); } }); }
Example #14
Source File: BrokerBasedLogTest.java From openmessaging-connect-runtime with Apache License 2.0 | 5 votes |
@Test public void testStart() { brokerBasedLog.start(); verify(producer, times(1)).startup(); verify(consumer, times(1)).attachQueue(anyString(), any(MessageListener.class)); verify(consumer, times(1)).startup(); }
Example #15
Source File: PushConsumerImpl.java From rocketmq-read with Apache License 2.0 | 5 votes |
@Override public PushConsumer attachQueue(final String queueName, final MessageListener listener) { this.subscribeTable.put(queueName, listener); try { this.rocketmqPushConsumer.subscribe(queueName, "*"); } catch (MQClientException e) { throw new OMSRuntimeException("-1", String.format("RocketMQ push consumer can't attach to %s.", queueName)); } return this; }
Example #16
Source File: SimplePushConsumer.java From rocketmq-read with Apache License 2.0 | 5 votes |
public static void main(String[] args) { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://localhost:9876/default:default"); final PushConsumer consumer = messagingAccessPoint. createPushConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "OMS_CONSUMER")); messagingAccessPoint.startup(); System.out.printf("MessagingAccessPoint startup OK%n"); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { consumer.shutdown(); messagingAccessPoint.shutdown(); } })); consumer.attachQueue("OMS_HELLO_TOPIC", new MessageListener() { @Override public void onReceived(Message message, Context context) { System.out.printf("Received one message: %s%n", message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)); context.ack(); } }); consumer.startup(); System.out.printf("Consumer startup OK%n"); }
Example #17
Source File: SimplePushConsumer.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public static void main(String[] args) { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://localhost:9876/default:default"); final PushConsumer consumer = messagingAccessPoint. createPushConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "OMS_CONSUMER")); messagingAccessPoint.startup(); System.out.printf("MessagingAccessPoint startup OK%n"); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { consumer.shutdown(); messagingAccessPoint.shutdown(); } })); consumer.attachQueue("OMS_HELLO_TOPIC", new MessageListener() { @Override public void onReceived(Message message, Context context) { System.out.printf("Received one message: %s%n", message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)); context.ack(); } }); consumer.startup(); System.out.printf("Consumer startup OK%n"); }
Example #18
Source File: ConsumerWrapper.java From joyqueue with Apache License 2.0 | 4 votes |
@Override public void bindQueue(String queueName, MessageListener listener) { delegate.bindQueue(queueName, listener); }
Example #19
Source File: SimpleMessageListener2.java From joyqueue with Apache License 2.0 | 4 votes |
@Override public void onReceived(Message message, MessageListener.Context context) { System.out.println(String.format("receive, message: %s", message)); context.ack(); }
Example #20
Source File: SimpleMessageListener1.java From joyqueue with Apache License 2.0 | 4 votes |
@OMSMessageListener(queueName = "test_topic_0") public void onReceived(Message message, MessageListener.Context context) { System.out.println(String.format("receive, message: %s", message)); context.ack(); }
Example #21
Source File: MessageListener1.java From joyqueue with Apache License 2.0 | 4 votes |
@OMSMessageListener(queueName = "test_topic_0") public void onReceived(Message message, MessageListener.Context context) { System.out.println(String.format("receive, message: %s", message)); context.ack(); }
Example #22
Source File: MessageListener2.java From joyqueue with Apache License 2.0 | 4 votes |
@Override public void onReceived(Message message, MessageListener.Context context) { System.out.println(String.format("receive, message: %s", message)); context.ack(); }
Example #23
Source File: PushConsumerImpl.java From rocketmq-read with Apache License 2.0 | 4 votes |
@Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList, ConsumeConcurrentlyContext contextRMQ) { MessageExt rmqMsg = rmqMsgList.get(0); BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg); MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic()); if (listener == null) { throw new OMSRuntimeException("-1", String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic())); } final KeyValue contextProperties = OMS.newKeyValue(); final CountDownLatch sync = new CountDownLatch(1); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name()); MessageListener.Context context = new MessageListener.Context() { @Override public KeyValue attributes() { return contextProperties; } @Override public void ack() { sync.countDown(); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); } }; long begin = System.currentTimeMillis(); listener.onReceived(omsMsg, context); long costs = System.currentTimeMillis() - begin; long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; try { sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); } catch (InterruptedException ignore) { } return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); }
Example #24
Source File: PushConsumerImpl.java From rocketmq-read with Apache License 2.0 | 4 votes |
@Override public PushConsumer attachQueue(String queueName, MessageListener listener, KeyValue attributes) { return this.attachQueue(queueName, listener); }
Example #25
Source File: PushConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 4 votes |
@Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList, ConsumeConcurrentlyContext contextRMQ) { MessageExt rmqMsg = rmqMsgList.get(0); BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg); MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic()); if (listener == null) { throw new OMSRuntimeException("-1", String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic())); } final KeyValue contextProperties = OMS.newKeyValue(); // 让一段代码执行完毕后再往下执行 final CountDownLatch sync = new CountDownLatch(1); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name()); MessageListener.Context context = new MessageListener.Context() { @Override public KeyValue attributes() { return contextProperties; } @Override public void ack() { sync.countDown(); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); } }; long begin = System.currentTimeMillis(); listener.onReceived(omsMsg, context); long costs = System.currentTimeMillis() - begin; long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; try { sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); } catch (InterruptedException ignore) { } return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); }
Example #26
Source File: PushConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 4 votes |
@Override public PushConsumer attachQueue(String queueName, MessageListener listener, KeyValue attributes) { return this.attachQueue(queueName, listener); }
Example #27
Source File: PushConsumerImpl.java From rocketmq with Apache License 2.0 | 4 votes |
@Override public PushConsumer attachQueue(String queueName, MessageListener listener, KeyValue attributes) { return this.attachQueue(queueName, listener); }
Example #28
Source File: PushConsumerImpl.java From rocketmq with Apache License 2.0 | 4 votes |
@Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList, ConsumeConcurrentlyContext contextRMQ) { MessageExt rmqMsg = rmqMsgList.get(0); BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg); MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic()); if (listener == null) { throw new OMSRuntimeException("-1", String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic())); } final KeyValue contextProperties = OMS.newKeyValue(); final CountDownLatch sync = new CountDownLatch(1); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name()); MessageListener.Context context = new MessageListener.Context() { @Override public KeyValue attributes() { return contextProperties; } @Override public void ack() { sync.countDown(); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); } }; long begin = System.currentTimeMillis(); listener.onReceived(omsMsg, context); long costs = System.currentTimeMillis() - begin; long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; try { sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); } catch (InterruptedException ignore) { } return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); }