Java Code Examples for io.openmessaging.KeyValue#put()
The following examples show how to use
io.openmessaging.KeyValue#put() .
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: V6ProducerTester.java From coding-snippets with MIT License | 6 votes |
public void init() { //init producer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class producerClass = Class.forName("io.openmessaging.v6.V6Producer"); producer = (Producer) producerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (producer == null) { throw new InstantiationException("Init Producer Failed"); } } catch (Exception e) { logger.error("please check the package name and class name:", e); } //init offsets,topic0-9,queue0-9 for (int i = 0; i < 10; i++) { // offsets.put("TOPIC_" + i, 0); offsets.put("QUEUE_" + i, 0); } for (int i = 0; i < 100; i++) { offsets.put("TOPIC_" + i, 0); } }
Example 3
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 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: ProducerTester.java From OpenMessageShaping with MIT License | 6 votes |
public void init() { //init producer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class producerClass = Class.forName("io.openmessaging.demo.DefaultProducer"); producer = (Producer) producerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (producer == null) { throw new InstantiationException("Init Producer Failed"); } } catch (Exception e) { logger.error("please check the package name and class name:", e); } //init offsets for (int i = 0; i < 10; i++) { offsets.put("TOPIC_" + i, 0); offsets.put("QUEUE_" + i, 0); } }
Example 6
Source File: AbstractOMSProducer.java From DDMQ 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); String accessPoints = clientConfig.getOmsAccessPoints(); 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.getOmsOperationTimeout()); this.rocketmqProducer.setInstanceName(producerId); this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); properties.put(PropertyKeys.PRODUCER_ID, producerId); }
Example 7
Source File: V2ProducerTester.java From coding-snippets with MIT License | 6 votes |
public void init() { //init producer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class producerClass = Class.forName("io.openmessaging.v2.V2Producer"); producer = (Producer) producerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (producer == null) { throw new InstantiationException("Init Producer Failed"); } } catch (Exception e) { logger.error("please check the package name and class name:", e); } //init offsets,topic0-9,queue0-9 for (int i = 0; i < 10; i++) { offsets.put("TOPIC_" + i, 0); offsets.put("QUEUE_" + i, 0); } }
Example 8
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 9
Source File: ConsumerTester.java From coding-snippets with MIT License | 6 votes |
public void init() { //init consumer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer"); consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (consumer == null) { throw new InstantiationException("Init Producer Failed"); } consumer.attachQueue(queue, topics); } catch (Exception e) { logger.error("please check the package name and class name:", e); } //init offsets offsets.put(queue, new HashMap<>()); for (String topic: topics) { offsets.put(topic, new HashMap<>()); } for (Map<String, Integer> map: offsets.values()) { for (int i = 0; i < Constants.PRO_NUM; i++) { map.put(Constants.PRO_PRE + i, 0); } } }
Example 10
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 11
Source File: PullConsumerImpl.java From rocketmq-all-4.1.0-incubating with Apache License 2.0 | 5 votes |
public PullConsumerImpl(final String queueName, final KeyValue properties) { this.properties = properties; this.targetQueueName = queueName; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); String consumerGroup = clientConfig.getRmqConsumerGroup(); 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(); String accessPoints = clientConfig.getOmsAccessPoints(); 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(PropertyKeys.CONSUMER_ID, consumerId); this.localMessageCache = new LocalMessageCache(this.rocketmqPullConsumer, clientConfig); }
Example 12
Source File: OMSUtil.java From rocketmq-4.3.0 with Apache License 2.0 | 5 votes |
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) { BytesMessage omsMsg = new BytesMessageImpl(); omsMsg.setBody(rmqMsg.getBody()); KeyValue headers = omsMsg.sysHeaders(); KeyValue properties = omsMsg.userHeaders(); final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet(); for (final Map.Entry<String, String> entry : entries) { if (isOMSHeader(entry.getKey())) { headers.put(entry.getKey(), entry.getValue()); } else { properties.put(entry.getKey(), entry.getValue()); } } omsMsg.putSysHeaders(BuiltinKeys.MESSAGE_ID, rmqMsg.getMsgId()); omsMsg.putSysHeaders(BuiltinKeys.DESTINATION, rmqMsg.getTopic()); omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys()); omsMsg.putSysHeaders(BuiltinKeys.BORN_HOST, String.valueOf(rmqMsg.getBornHost())); omsMsg.putSysHeaders(BuiltinKeys.BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); omsMsg.putSysHeaders(BuiltinKeys.STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); omsMsg.putSysHeaders(BuiltinKeys.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp()); return omsMsg; }
Example 13
Source File: V4ConsumerTester.java From coding-snippets with MIT License | 5 votes |
public void init() { //init consumer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class consumerClass = Class.forName("io.openmessaging.v4.V4PullConsumer"); consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (consumer == null) { throw new InstantiationException("Init Producer Failed"); } consumer.attachQueue(queue, topics); } catch (Exception e) { logger.error("please check the package name and class name:", e); } /** * init offsets,为了检验正确性和顺序。 * key为本线程消费的queue/topic, * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。 */ offsets.put(queue, new HashMap<>()); for (String topic: topics) { offsets.put(topic, new HashMap<>()); } for (Map<String, Integer> map: offsets.values()) { for (int i = 0; i < Constants.PRO_NUM; i++) { //"PRODUCER_i",即生产者线程名 map.put(Constants.PRO_PRE + i, 0); } } }
Example 14
Source File: ConsumerTester.java From coding-snippets with MIT License | 5 votes |
public void init() { //init consumer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class consumerClass = Class.forName("io.openmessaging.demo.DefaultPullConsumer"); consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (consumer == null) { throw new InstantiationException("Init Producer Failed"); } consumer.attachQueue(queue, topics); } catch (Exception e) { logger.error("please check the package name and class name:", e); } /** * init offsets,为了检验正确性和顺序。 * key为本线程消费的queue/topic, * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。 */ offsets.put(queue, new HashMap<>()); for (String topic: topics) { offsets.put(topic, new HashMap<>()); } for (Map<String, Integer> map: offsets.values()) { for (int i = 0; i < Constants.PRO_NUM; i++) { //"PRODUCER_i",即生产者线程名 map.put(Constants.PRO_PRE + i, 0); } } }
Example 15
Source File: V2ConsumerTester.java From coding-snippets with MIT License | 5 votes |
public void init() { //init consumer try { Class kvClass = Class.forName("io.openmessaging.demo.DefaultKeyValue"); KeyValue keyValue = (KeyValue) kvClass.newInstance(); keyValue.put("STORE_PATH", Constants.STORE_PATH); Class consumerClass = Class.forName("io.openmessaging.v2.V2PullConsumer"); consumer = (PullConsumer) consumerClass.getConstructor(new Class[]{KeyValue.class}).newInstance(new Object[]{keyValue}); if (consumer == null) { throw new InstantiationException("Init Producer Failed"); } consumer.attachQueue(queue, topics); } catch (Exception e) { logger.error("please check the package name and class name:", e); } /** * init offsets,为了检验正确性和顺序。 * key为本线程消费的queue/topic, * value为对应的线程和该线程发送的消息的偏移(key为线程名, value初始为0)。 */ offsets.put(queue, new HashMap<>()); for (String topic: topics) { offsets.put(topic, new HashMap<>()); } for (Map<String, Integer> map: offsets.values()) { for (int i = 0; i < Constants.PRO_NUM; i++) { //"PRODUCER_i",即生产者线程名 map.put(Constants.PRO_PRE + i, 0); } } }
Example 16
Source File: OMSUtil.java From rocketmq-read with Apache License 2.0 | 5 votes |
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) { BytesMessage omsMsg = new BytesMessageImpl(); omsMsg.setBody(rmqMsg.getBody()); KeyValue headers = omsMsg.sysHeaders(); KeyValue properties = omsMsg.userHeaders(); final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet(); for (final Map.Entry<String, String> entry : entries) { if (isOMSHeader(entry.getKey())) { headers.put(entry.getKey(), entry.getValue()); } else { properties.put(entry.getKey(), entry.getValue()); } } omsMsg.putSysHeaders(BuiltinKeys.MESSAGE_ID, rmqMsg.getMsgId()); omsMsg.putSysHeaders(BuiltinKeys.DESTINATION, rmqMsg.getTopic()); omsMsg.putSysHeaders(BuiltinKeys.SEARCH_KEYS, rmqMsg.getKeys()); omsMsg.putSysHeaders(BuiltinKeys.BORN_HOST, String.valueOf(rmqMsg.getBornHost())); omsMsg.putSysHeaders(BuiltinKeys.BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); omsMsg.putSysHeaders(BuiltinKeys.STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); omsMsg.putSysHeaders(BuiltinKeys.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp()); return omsMsg; }
Example 17
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 18
Source File: AccessPointContainer.java From joyqueue with Apache License 2.0 | 5 votes |
public AccessPointContainer(String id, String url, List<KeyValueAttribute> attributes) { KeyValue keyValue = OMS.newKeyValue(); for (KeyValueAttribute attribute : attributes) { keyValue.put(attribute.getKey(), attribute.getValue()); } this.id = id; this.url = url; this.attributes = keyValue; }
Example 19
Source File: ConsoleProducer.java From joyqueue with Apache License 2.0 | 5 votes |
protected static Producer buildProducer(ConsoleProducerConfig config) { KeyValue keyValue = OMS.newKeyValue(); keyValue.put(OMSBuiltinKeys.ACCOUNT_KEY, config.getToken()); keyValue.put(JoyQueueNameServerBuiltinKeys.NAMESPACE, StringUtils.defaultIfBlank(config.getNamespace(), ToolConsts.DEFAULT_NAMESPACE)); for (Map.Entry<String, String> entry : config.getParams().entrySet()) { keyValue.put(entry.getKey(), entry.getValue()); } MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint( String.format("oms:%s://%s@%s/%s", ToolConsts.DRIVER, config.getApp(), config.getBootstrap(), StringUtils.defaultIfBlank(config.getRegion(), ToolConsts.DEFAULT_REGION)), keyValue); return messagingAccessPoint.createProducer(); }
Example 20
Source File: PullConsumerImpl.java From DDMQ with Apache License 2.0 | 5 votes |
public PullConsumerImpl(final String queueName, final KeyValue properties) { this.properties = properties; this.targetQueueName = queueName; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); String consumerGroup = clientConfig.getRmqConsumerGroup(); 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(); String accessPoints = clientConfig.getOmsAccessPoints(); 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(PropertyKeys.CONSUMER_ID, consumerId); this.localMessageCache = new LocalMessageCache(this.rocketmqPullConsumer, clientConfig); }