io.openmessaging.OMSBuiltinKeys Java Examples
The following examples show how to use
io.openmessaging.OMSBuiltinKeys.
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: AsyncProducer.java From joyqueue with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); keyValue.put(JoyQueueBuiltinKeys.TRANSACTION_TIMEOUT, 1000 * 10); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Producer producer = messagingAccessPoint.createProducer(); producer.start(); Message message = producer.createMessage("test_topic_0", "body".getBytes()); Future<SendResult> future = producer.sendAsync(message); // future.addListener(new FutureListener<SendResult>() { //// @Override //// public void operationComplete(Future<SendResult> future) { //// System.out.println(future.get().messageId()); //// } //// }); System.out.println(future.get().messageId()); }
Example #2
Source File: PullConsumerImplTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPullConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); consumer.attachQueue(queueName); Field field = PullConsumerImpl.class.getDeclaredField("rocketmqPullConsumer"); field.setAccessible(true); field.set(consumer, rocketmqPullConsumer); //Replace ClientConfig clientConfig = new ClientConfig(); clientConfig.setOperationTimeout(200); localMessageCache = spy(new LocalMessageCache(rocketmqPullConsumer, clientConfig)); field = PullConsumerImpl.class.getDeclaredField("localMessageCache"); field.setAccessible(true); field.set(consumer, localMessageCache); messagingAccessPoint.startup(); consumer.startup(); }
Example #3
Source File: PushConsumerImplTest.java From rocketmq with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPushConsumer( OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); Field field = PushConsumerImpl.class.getDeclaredField("rocketmqPushConsumer"); field.setAccessible(true); DefaultMQPushConsumer innerConsumer = (DefaultMQPushConsumer) field.get(consumer); field.set(consumer, rocketmqPushConsumer); //Replace when(rocketmqPushConsumer.getMessageListener()).thenReturn(innerConsumer.getMessageListener()); messagingAccessPoint.startup(); consumer.startup(); }
Example #4
Source File: AbstractOMSProducer.java From rocketmq with Apache License 2.0 | 6 votes |
AbstractOMSProducer(final KeyValue properties) { this.properties = properties; this.rocketmqProducer = new DefaultMQProducer(); this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';')); } this.rocketmqProducer.setProducerGroup(clientConfig.getRmqProducerGroup()); String producerId = buildInstanceName(); this.rocketmqProducer.setSendMsgTimeout(clientConfig.getOperationTimeout()); this.rocketmqProducer.setInstanceName(producerId); this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); this.rocketmqProducer.setLanguage(LanguageCode.OMS); properties.put(OMSBuiltinKeys.PRODUCER_ID, producerId); }
Example #5
Source File: MessagingAccessPointAdapter.java From openmessaging-java with Apache License 2.0 | 6 votes |
/** * Returns a {@code MessagingAccessPoint} instance from the specified OMS driver URL with some preset userHeaders. * * @param url the driver URL. * @param attributes the preset userHeaders. * @return a {@code MessagingAccessPoint} instance. * @throws OMSRuntimeException if the adapter fails to create a {@code MessagingAccessPoint} instance from the URL. */ public static MessagingAccessPoint getMessagingAccessPoint(String url, KeyValue attributes) { AccessPointURI accessPointURI = new AccessPointURI(url); String driverImpl = parseDriverImpl(accessPointURI.getDriverType(), attributes); attributes.put(OMSBuiltinKeys.ACCESS_POINTS, accessPointURI.getHosts()); attributes.put(OMSBuiltinKeys.DRIVER_IMPL, driverImpl); attributes.put(OMSBuiltinKeys.REGION, accessPointURI.getRegion()); attributes.put(OMSBuiltinKeys.ACCOUNT_ID, accessPointURI.getAccountId()); try { Class<?> driverImplClass = Class.forName(driverImpl); Constructor constructor = driverImplClass.getConstructor(KeyValue.class); MessagingAccessPoint vendorImpl = (MessagingAccessPoint) constructor.newInstance(attributes); checkSpecVersion(OMS.specVersion, vendorImpl.version()); return vendorImpl; } catch (Throwable e) { throw generateException(OMSResponseStatus.STATUS_10000, url); } }
Example #6
Source File: SimpleMetadata.java From joyqueue with Apache License 2.0 | 6 votes |
public static void main(String[] args) { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Producer producer = messagingAccessPoint.createProducer(); producer.start(); QueueMetaData queueMetaData = producer.getQueueMetaData("test_topic_0"); for (QueueMetaData.Partition partition : queueMetaData.partitions()) { System.out.println(String.format("partition: %s, partitionHost: %s", partition.partitionId(), partition.partitonHost())); } Message message = producer.createMessage("test_topic_0", "body".getBytes()); SendResult sendResult = producer.send(message); System.out.println(String.format("messageId: %s", sendResult.messageId())); }
Example #7
Source File: BatchConsumer.java From joyqueue with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Consumer consumer = messagingAccessPoint.createConsumer(); consumer.bindQueue("test_topic_0", new BatchMessageListener() { @Override public void onReceived(List<Message> messages, Context context) { for (Message message : messages) { System.out.println(String.format("onReceived, message: %s", message)); } // 代表这一批消息的ack context.ack(); } }); consumer.start(); System.in.read(); }
Example #8
Source File: BatchProducer.java From joyqueue with Apache License 2.0 | 6 votes |
public static void main(String[] args) { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Producer producer = messagingAccessPoint.createProducer(); producer.start(); List<Message> messages = new ArrayList<>(); for (int i = 0; i < 10; i++) { Message message = producer.createMessage("test_topic_0", "body".getBytes()); messages.add(message); } producer.send(messages); }
Example #9
Source File: AsyncBatchProducer.java From joyqueue with Apache License 2.0 | 6 votes |
public static void main(String[] args) { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Producer producer = messagingAccessPoint.createProducer(); producer.start(); List<Message> messages = new ArrayList<>(); for (int i = 0; i < 10; i++) { Message message = producer.createMessage("test_topic_0", "body".getBytes()); messages.add(message); } Future<SendResult> future = producer.sendAsync(messages); // future.addListener(new FutureListener<SendResult>() { // @Override // public void operationComplete(Future<SendResult> future) { // System.out.println(future.get().messageId()); // } // }); System.out.println(future.get().messageId()); }
Example #10
Source File: PullConsumerImplTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPullConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); consumer.attachQueue(queueName); Field field = PullConsumerImpl.class.getDeclaredField("rocketmqPullConsumer"); field.setAccessible(true); field.set(consumer, rocketmqPullConsumer); //Replace ClientConfig clientConfig = new ClientConfig(); clientConfig.setOperationTimeout(200); localMessageCache = spy(new LocalMessageCache(rocketmqPullConsumer, clientConfig)); field = PullConsumerImpl.class.getDeclaredField("localMessageCache"); field.setAccessible(true); field.set(consumer, localMessageCache); messagingAccessPoint.startup(); consumer.startup(); }
Example #11
Source File: PushConsumerImplTest.java From rocketmq-read with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPushConsumer( OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); Field field = PushConsumerImpl.class.getDeclaredField("rocketmqPushConsumer"); field.setAccessible(true); DefaultMQPushConsumer innerConsumer = (DefaultMQPushConsumer) field.get(consumer); field.set(consumer, rocketmqPushConsumer); //Replace when(rocketmqPushConsumer.getMessageListener()).thenReturn(innerConsumer.getMessageListener()); messagingAccessPoint.startup(); consumer.startup(); }
Example #12
Source File: AbstractOMSProducer.java From rocketmq-read with Apache License 2.0 | 6 votes |
AbstractOMSProducer(final KeyValue properties) { this.properties = properties; this.rocketmqProducer = new DefaultMQProducer(); this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';')); } this.rocketmqProducer.setProducerGroup(clientConfig.getRmqProducerGroup()); String producerId = buildInstanceName(); this.rocketmqProducer.setSendMsgTimeout(clientConfig.getOperationTimeout()); this.rocketmqProducer.setInstanceName(producerId); this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); this.rocketmqProducer.setLanguage(LanguageCode.OMS); properties.put(OMSBuiltinKeys.PRODUCER_ID, producerId); }
Example #13
Source File: AbstractOMSProducer.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
AbstractOMSProducer(final KeyValue properties) { this.properties = properties; this.rocketmqProducer = new DefaultMQProducer(); this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';')); } this.rocketmqProducer.setProducerGroup(clientConfig.getRmqProducerGroup()); String producerId = buildInstanceName(); this.rocketmqProducer.setSendMsgTimeout(clientConfig.getOperationTimeout()); this.rocketmqProducer.setInstanceName(producerId); this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); this.rocketmqProducer.setLanguage(LanguageCode.OMS); properties.put(OMSBuiltinKeys.PRODUCER_ID, producerId); }
Example #14
Source File: PushConsumerImplTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPushConsumer( OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); Field field = PushConsumerImpl.class.getDeclaredField("rocketmqPushConsumer"); field.setAccessible(true); DefaultMQPushConsumer innerConsumer = (DefaultMQPushConsumer) field.get(consumer); field.set(consumer, rocketmqPushConsumer); //Replace when(rocketmqPushConsumer.getMessageListener()).thenReturn(innerConsumer.getMessageListener()); messagingAccessPoint.startup(); consumer.startup(); }
Example #15
Source File: PullConsumerImplTest.java From rocketmq-4.3.0 with Apache License 2.0 | 6 votes |
@Before public void init() throws NoSuchFieldException, IllegalAccessException { final MessagingAccessPoint messagingAccessPoint = OMS .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); consumer = messagingAccessPoint.createPullConsumer(OMS.newKeyValue().put(OMSBuiltinKeys.CONSUMER_ID, "TestGroup")); consumer.attachQueue(queueName); Field field = PullConsumerImpl.class.getDeclaredField("rocketmqPullConsumer"); field.setAccessible(true); field.set(consumer, rocketmqPullConsumer); //Replace ClientConfig clientConfig = new ClientConfig(); clientConfig.setOperationTimeout(200); localMessageCache = spy(new LocalMessageCache(rocketmqPullConsumer, clientConfig)); field = PullConsumerImpl.class.getDeclaredField("localMessageCache"); field.setAccessible(true); field.set(consumer, localMessageCache); messagingAccessPoint.startup(); consumer.startup(); }
Example #16
Source File: ReceiveConsumer.java From joyqueue with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Consumer consumer = messagingAccessPoint.createConsumer(); consumer.start(); // 绑定主题,将要消费的主题 consumer.bindQueue("test_topic_0"); while (true) { System.out.println("doReceive"); // 拉取单条消息 // 参数是超时时间,只是网络请求的超时 Message message = consumer.receive(1000 * 10); // 没有拉取到,继续循环 if (message == null) { continue; } // 拉取到消息,打印日志并ack System.out.println(String.format("receive, message: %s", message)); consumer.ack(message.getMessageReceipt()); Thread.currentThread().sleep(1000 * 1); } }
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: PushConsumerImpl.java From rocketmq with Apache License 2.0 | 5 votes |
public PushConsumerImpl(final KeyValue properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPushConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); this.rocketmqPushConsumer.registerMessageListener(new MessageListenerImpl()); }
Example #19
Source File: PullConsumerImpl.java From rocketmq with Apache License 2.0 | 5 votes |
public PullConsumerImpl(final KeyValue properties) { this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } pullConsumerScheduleService = new MQPullConsumerScheduleService(consumerGroup); this.rocketmqPullConsumer = pullConsumerScheduleService.getDefaultMQPullConsumer(); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPullConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } this.rocketmqPullConsumer.setConsumerGroup(consumerGroup); int maxReDeliveryTimes = clientConfig.getRmqMaxRedeliveryTimes(); this.rocketmqPullConsumer.setMaxReconsumeTimes(maxReDeliveryTimes); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPullConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPullConsumer.setLanguage(LanguageCode.OMS); this.localMessageCache = new LocalMessageCache(this.rocketmqPullConsumer, clientConfig); }
Example #20
Source File: PullConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public PullConsumerImpl(final KeyValue properties) { this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } pullConsumerScheduleService = new MQPullConsumerScheduleService(consumerGroup); this.rocketmqPullConsumer = pullConsumerScheduleService.getDefaultMQPullConsumer(); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPullConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } this.rocketmqPullConsumer.setConsumerGroup(consumerGroup); int maxReDeliveryTimes = clientConfig.getRmqMaxRedeliveryTimes(); this.rocketmqPullConsumer.setMaxReconsumeTimes(maxReDeliveryTimes); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPullConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPullConsumer.setLanguage(LanguageCode.OMS); this.localMessageCache = new LocalMessageCache(this.rocketmqPullConsumer, clientConfig); }
Example #21
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 #22
Source File: MessagingAccessPointAdapterTest.java From openmessaging-java with Apache License 2.0 | 5 votes |
@Test public void getMessagingAccessPoint() { String testURI = "oms:test-vendor://[email protected]/us-east:default_space"; KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.DRIVER_IMPL, "io.openmessaging.internal.TestVendor"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(testURI, keyValue); assertThat(messagingAccessPoint).isExactlyInstanceOf(TestVendor.class); }
Example #23
Source File: PushConsumerImpl.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public PushConsumerImpl(final KeyValue properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPushConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); this.rocketmqPushConsumer.registerMessageListener(new MessageListenerImpl()); }
Example #24
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 #25
Source File: BatchReceiveConsumer.java From joyqueue with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); Consumer consumer = messagingAccessPoint.createConsumer(); consumer.start(); // 绑定主题,将要消费的主题 consumer.bindQueue("test_topic_0"); // 这里只是简单例子,根据具体情况进行调度处理 // 参数是超时时间,只是网络请求的超时 while (true) { System.out.println("doReceive"); // 批量拉取消息 List<Message> messages = consumer.batchReceive(1000 * 10); if (CollectionUtils.isEmpty(messages)) { continue; } for (Message message : messages) { // 拉取到消息,打印日志并ack System.out.println(String.format("receive, message: %s", messages)); consumer.ack(message.getMessageReceipt()); } Thread.currentThread().sleep(1000 * 1); } }
Example #26
Source File: PushConsumerImpl.java From rocketmq-read with Apache License 2.0 | 5 votes |
public PushConsumerImpl(final KeyValue properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); if ("true".equalsIgnoreCase(System.getenv("OMS_RMQ_DIRECT_NAME_SRV"))) { String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); } this.rocketmqPushConsumer.setNamesrvAddr(accessPoints.replace(',', ';')); } String consumerGroup = clientConfig.getConsumerId(); if (null == consumerGroup || consumerGroup.isEmpty()) { throw new OMSRuntimeException("-1", "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); this.rocketmqPushConsumer.setConsumeTimeout(clientConfig.getRmqMessageConsumeTimeout()); this.rocketmqPushConsumer.setConsumeThreadMax(clientConfig.getRmqMaxConsumeThreadNums()); this.rocketmqPushConsumer.setConsumeThreadMin(clientConfig.getRmqMinConsumeThreadNums()); String consumerId = OMSUtil.buildInstanceName(); this.rocketmqPushConsumer.setInstanceName(consumerId); properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); this.rocketmqPushConsumer.registerMessageListener(new MessageListenerImpl()); }
Example #27
Source File: SimpleProducer.java From joyqueue with Apache License 2.0 | 5 votes |
public static void main(String[] args) { 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); // 使用MessagingAccessPoint创建producer Producer producer = messagingAccessPoint.createProducer(); producer.start(); // 使用producer.createMessage方法创建message Message message = producer.createMessage(topic, "Message body".getBytes()); // 设置messageKey,非必填 // 如果需要相对顺序消息,也可以使用messageKey作为key指定分区 // message.extensionHeader().get().setMessageKey("test_key"); // 自定义发送的partition // 最好根据元数据自定义分配,不要写死partitions // List<QueueMetaData.Partition> partitions = producer.getQueueMetaData("test_topic_0").partitions(); // QueueMetaData.Partition partition = partitions.get((int) SystemClock.now() % partitions.size()); // message.extensionHeader().get().setPartition(partition.partitionId()); // 生产消息,不抛异常就算成功,sendResult里的messageId暂时没有意义 SendResult sendResult = producer.send(message); // 打印生产结果 System.out.println(String.format("messageId: %s", sendResult.messageId())); }
Example #28
Source File: ExtensionTransactionProducer.java From joyqueue with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, "test_token"); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint("oms:joyqueue://[email protected]:50088/UNKNOWN", keyValue); ExtensionProducer extensionProducer = (ExtensionProducer) messagingAccessPoint.createProducer(); extensionProducer.start(); ExtensionTransactionalResult transactionalResult = extensionProducer.prepare(); for (int i = 0; i < 10; i++) { // 可以发多条事务消息 Message message = extensionProducer.createMessage("test_topic_0", "body".getBytes()); // 添加事务id,设置过事务id的才会被补偿,补偿时会带上这个事务id,非必填 // 建议根据业务使用有意义的事务id message.extensionHeader().get().setTransactionId("test_transactionId"); SendResult sendResult = transactionalResult.send(message); System.out.println(sendResult.messageId()); } // 提交事务 transactionalResult.commit(); // 回滚事务 // transactionalResult.rollback(); }
Example #29
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 #30
Source File: TopicMsgFilterServiceImpl.java From joyqueue with Apache License 2.0 | 5 votes |
private ExtensionConsumer initConsumer(TopicMsgFilter msgFilter) { final String url = String.format(urlFormat, msgFilter.getApp(), msgFilter.getBrokerAddr()); KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, msgFilter.getToken()); keyValue.put(JoyQueueBuiltinKeys.IO_THREADS, 1); keyValue.put(JoyQueueBuiltinKeys.BATCH_SIZE, 100); MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(url, keyValue); ExtensionConsumer consumer = (ExtensionConsumer) messagingAccessPoint.createConsumer(); consumer.bindQueue(msgFilter.getTopic()); return consumer; }