diff --git a/build.gradle b/build.gradle index 19a9b26d21..a97c14e877 100644 --- a/build.gradle +++ b/build.gradle @@ -240,6 +240,8 @@ subprojects { exclude 'eventmesh-connector-api*.jar' exclude 'eventmesh-registry*.jar' exclude 'eventmesh-starter*.jar' + exclude 'eventmesh-test*.jar' + exclude 'eventmesh-sdk*.jar' } copy { into '../dist/lib' diff --git a/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md b/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md index 1c868330df..bb87452509 100644 --- a/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md +++ b/docs/cn/instructions/eventmesh-runtime-quickstart.zh-CN.md @@ -192,7 +192,7 @@ docker run -d -p 10911:10911 -p 10909:10909 -v `pwd`/data/broker/logs:/root/logs Windows -- Windows系统下运行示例可以参考[这里](https://github.com/WeBankFinTech/EventMesh/blob/develop/docs/en/instructions/eventmesh-sdk-java-quickstart.md) +- Windows系统下运行示例可以参考[这里](https://github.com/WeBankFinTech/EventMesh/blob/develop/docs/cn/instructions/eventmesh-sdk-java-quickstart.zh-CN.md) Linux diff --git a/eventmesh-connector-api/build.gradle b/eventmesh-connector-api/build.gradle index 77543b3cff..bf71a34be7 100644 --- a/eventmesh-connector-api/build.gradle +++ b/eventmesh-connector-api/build.gradle @@ -42,7 +42,7 @@ repositories { } List open_message = [ - "io.openmessaging:openmessaging-api:0.3.1-alpha" + "io.openmessaging:openmessaging-api:2.2.1-pubsub" ] dependencies { diff --git a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/RRCallback.java b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/RRCallback.java index 9f0297df1a..d72ccbbca2 100644 --- a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/RRCallback.java +++ b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/RRCallback.java @@ -1,6 +1,6 @@ package com.webank.eventmesh.api; -import io.openmessaging.Message; +import io.openmessaging.api.Message; public interface RRCallback { diff --git a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/SendCallback.java b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/SendCallback.java deleted file mode 100644 index d010ad02a7..0000000000 --- a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/SendCallback.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.webank.eventmesh.api; - -import io.openmessaging.producer.SendResult; - -public interface SendCallback { - - void onSuccess(final SendResult sendResult); - - void onException(final Throwable e); - -} diff --git a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/consumer/MeshMQPushConsumer.java b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/consumer/MeshMQPushConsumer.java index b5ad85c019..a18ce443b5 100644 --- a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/consumer/MeshMQPushConsumer.java +++ b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/consumer/MeshMQPushConsumer.java @@ -1,18 +1,20 @@ package com.webank.eventmesh.api.consumer; import com.webank.eventmesh.api.AbstractContext; -import io.openmessaging.KeyValue; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.Message; -import io.openmessaging.consumer.PushConsumer; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.MessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.Consumer; import java.util.List; +import java.util.Properties; -public interface MeshMQPushConsumer extends PushConsumer { +public interface MeshMQPushConsumer extends Consumer { - void init(KeyValue keyValue) throws Exception; + void init(Properties keyValue) throws Exception; - void start() throws Exception; + @Override + void start(); // void updateOffset(List msgs, ConsumeConcurrentlyContext context); @@ -20,13 +22,10 @@ public interface MeshMQPushConsumer extends PushConsumer { // void registerMessageListener(MessageListenerConcurrently messageListenerConcurrently); - void subscribe(String topic, final MessageListener listener) throws Exception; + void subscribe(String topic, final AsyncMessageListener listener) throws Exception; - void unsubscribe(String topic) throws Exception; - - boolean isPause(); - - void pause(); + @Override + void unsubscribe(String topic); AbstractContext getContext(); } diff --git a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/producer/MeshMQProducer.java b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/producer/MeshMQProducer.java index 988796a345..f20a879f4b 100644 --- a/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/producer/MeshMQProducer.java +++ b/eventmesh-connector-api/src/main/java/com/webank/eventmesh/api/producer/MeshMQProducer.java @@ -18,16 +18,18 @@ package com.webank.eventmesh.api.producer; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.producer.Producer; +import io.openmessaging.api.Message; +import io.openmessaging.api.Producer; +import io.openmessaging.api.SendCallback; + +import java.util.Properties; public interface MeshMQProducer extends Producer { - void init(KeyValue keyValue) throws Exception; + void init(Properties properties) throws Exception; - void start() throws Exception; + @Override + void start(); void send(Message message, SendCallback sendCallback) throws Exception; diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java index 66c7ebb92f..79d6a5f17e 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/MessagingAccessPointImpl.java @@ -17,88 +17,75 @@ package com.webank.eventmesh.connector.rocketmq; import com.webank.eventmesh.connector.rocketmq.producer.ProducerImpl; -import com.webank.eventmesh.connector.rocketmq.consumer.PullConsumerImpl; import com.webank.eventmesh.connector.rocketmq.consumer.PushConsumerImpl; -import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; -import io.openmessaging.KeyValue; -import io.openmessaging.MessagingAccessPoint; -import io.openmessaging.ResourceManager; -import io.openmessaging.consumer.PullConsumer; -import io.openmessaging.consumer.PushConsumer; -import io.openmessaging.consumer.StreamingConsumer; -import io.openmessaging.exception.OMSNotSupportedException; -import io.openmessaging.producer.Producer; +import io.openmessaging.api.Consumer; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.Producer; +import io.openmessaging.api.PullConsumer; +import io.openmessaging.api.batch.BatchConsumer; +import io.openmessaging.api.order.OrderConsumer; +import io.openmessaging.api.order.OrderProducer; +import io.openmessaging.api.transaction.LocalTransactionChecker; +import io.openmessaging.api.transaction.TransactionProducer; + +import java.util.Properties; public class MessagingAccessPointImpl implements MessagingAccessPoint { - private KeyValue accessPointProperties; + private Properties accessPointProperties; - public MessagingAccessPointImpl(final KeyValue accessPointProperties) { + public MessagingAccessPointImpl(final Properties accessPointProperties) { this.accessPointProperties = accessPointProperties; } @Override - public KeyValue attributes() { - return accessPointProperties; + public String version() { + return null; } @Override - public String implVersion() { - return "0.3.0"; + public Properties attributes() { + return accessPointProperties; } @Override - public Producer createProducer() { + public Producer createProducer(Properties properties) { return new ProducerImpl(this.accessPointProperties); } @Override - public Producer createProducer(KeyValue properties) { - return new ProducerImpl(OMSUtil.buildKeyValue(this.accessPointProperties, properties)); - } - - @Override - public PushConsumer createPushConsumer() { - return new PushConsumerImpl(accessPointProperties); + public OrderProducer createOrderProducer(Properties properties) { + return null; } @Override - public PushConsumer createPushConsumer(KeyValue properties) { - return new PushConsumerImpl(OMSUtil.buildKeyValue(this.accessPointProperties, properties)); + public TransactionProducer createTransactionProducer(Properties properties, LocalTransactionChecker checker) { + return null; } @Override - public PullConsumer createPullConsumer() { - return new PullConsumerImpl(accessPointProperties); + public TransactionProducer createTransactionProducer(Properties properties) { + return null; } @Override - public PullConsumer createPullConsumer(KeyValue attributes) { - return new PullConsumerImpl(OMSUtil.buildKeyValue(this.accessPointProperties, attributes)); + public Consumer createConsumer(Properties properties) { + return new PushConsumerImpl(properties); } @Override - public StreamingConsumer createStreamingConsumer() { + public PullConsumer createPullConsumer(Properties properties) { return null; } @Override - public StreamingConsumer createStreamingConsumer(KeyValue attributes) { + public BatchConsumer createBatchConsumer(Properties properties) { return null; } @Override - public ResourceManager resourceManager() { - throw new OMSNotSupportedException("-1", "ResourceManager is not supported in current version."); - } - - @Override - public void startup() { - //Ignore + public OrderConsumer createOrderedConsumer(Properties properties) { + return null; } - @Override - public void shutdown() { - //Ignore - } } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/config/ClientConfig.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/config/ClientConfig.java index d2ce6119a5..9a0ebacce6 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/config/ClientConfig.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/config/ClientConfig.java @@ -17,7 +17,7 @@ package com.webank.eventmesh.connector.rocketmq.config; import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; -import io.openmessaging.OMSBuiltinKeys; +import io.openmessaging.api.OMSBuiltinKeys; public class ClientConfig implements OMSBuiltinKeys, NonStandardKeys { private String driverImpl; diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/LocalMessageCache.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/LocalMessageCache.java deleted file mode 100644 index eb2c0b8419..0000000000 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/LocalMessageCache.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.webank.eventmesh.connector.rocketmq.consumer; - -import com.webank.eventmesh.connector.rocketmq.domain.ConsumeRequest; -import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.ServiceLifecycle; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; -import org.apache.rocketmq.client.exception.MQClientException; -import org.apache.rocketmq.client.impl.consumer.ProcessQueue; -import org.apache.rocketmq.client.log.ClientLogger; -import org.apache.rocketmq.common.ThreadFactoryImpl; -import org.apache.rocketmq.common.message.MessageAccessor; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.message.MessageQueue; -import org.apache.rocketmq.common.utils.ThreadUtils; -import org.apache.rocketmq.logging.InternalLogger; - -import java.util.Collections; -import java.util.Map; -import java.util.TreeMap; -import java.util.concurrent.*; -import java.util.concurrent.locks.ReadWriteLock; - -public class LocalMessageCache implements ServiceLifecycle { - private final BlockingQueue consumeRequestCache; - private final Map consumedRequest; - private final ConcurrentHashMap pullOffsetTable; - private final DefaultMQPullConsumer rocketmqPullConsumer; - private final ClientConfig clientConfig; - private final ScheduledExecutorService cleanExpireMsgExecutors; - - private final static InternalLogger log = ClientLogger.getLog(); - - public LocalMessageCache(final DefaultMQPullConsumer rocketmqPullConsumer, final ClientConfig clientConfig) { - consumeRequestCache = new LinkedBlockingQueue<>(clientConfig.getRmqPullMessageCacheCapacity()); - this.consumedRequest = new ConcurrentHashMap<>(); - this.pullOffsetTable = new ConcurrentHashMap<>(); - this.rocketmqPullConsumer = rocketmqPullConsumer; - this.clientConfig = clientConfig; - this.cleanExpireMsgExecutors = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl( - "OMS_CleanExpireMsgScheduledThread_")); - } - - public int nextPullBatchNums() { - return Math.min(clientConfig.getRmqPullMessageBatchNums(), consumeRequestCache.remainingCapacity()); - } - - public long nextPullOffset(MessageQueue remoteQueue) { - if (!pullOffsetTable.containsKey(remoteQueue)) { - try { - pullOffsetTable.putIfAbsent(remoteQueue, - rocketmqPullConsumer.fetchConsumeOffset(remoteQueue, false)); - } catch (MQClientException e) { - log.error("An error occurred in fetch consume offset process.", e); - } - } - return pullOffsetTable.get(remoteQueue); - } - - public void updatePullOffset(MessageQueue remoteQueue, long nextPullOffset) { - pullOffsetTable.put(remoteQueue, nextPullOffset); - } - - public void submitConsumeRequest(ConsumeRequest consumeRequest) { - try { - consumeRequestCache.put(consumeRequest); - } catch (InterruptedException ignore) { - } - } - - public MessageExt poll() { - return poll(clientConfig.getOperationTimeout()); - } - - MessageExt poll(final KeyValue properties) { - int currentPollTimeout = clientConfig.getOperationTimeout(); - if (properties.containsKey(Message.BuiltinKeys.TIMEOUT)) { - currentPollTimeout = properties.getInt(Message.BuiltinKeys.TIMEOUT); - } - return poll(currentPollTimeout); - } - - private MessageExt poll(long timeout) { - try { - ConsumeRequest consumeRequest = consumeRequestCache.poll(timeout, TimeUnit.MILLISECONDS); - if (consumeRequest != null) { - MessageExt messageExt = consumeRequest.getMessageExt(); - consumeRequest.setStartConsumeTimeMillis(System.currentTimeMillis()); - MessageAccessor.setConsumeStartTimeStamp(messageExt, String.valueOf(consumeRequest.getStartConsumeTimeMillis())); - consumedRequest.put(messageExt.getMsgId(), consumeRequest); - return messageExt; - } - } catch (InterruptedException ignore) { - } - return null; - } - - void ack(final String messageId) { - ConsumeRequest consumeRequest = consumedRequest.remove(messageId); - if (consumeRequest != null) { - long offset = consumeRequest.getProcessQueue().removeMessage(Collections.singletonList(consumeRequest.getMessageExt())); - try { - rocketmqPullConsumer.updateConsumeOffset(consumeRequest.getMessageQueue(), offset); - } catch (MQClientException e) { - log.error("An error occurred in update consume offset process.", e); - } - } - } - - void ack(final MessageQueue messageQueue, final ProcessQueue processQueue, final MessageExt messageExt) { - consumedRequest.remove(messageExt.getMsgId()); - long offset = processQueue.removeMessage(Collections.singletonList(messageExt)); - try { - rocketmqPullConsumer.updateConsumeOffset(messageQueue, offset); - } catch (MQClientException e) { - log.error("An error occurred in update consume offset process.", e); - } - } - - @Override - public void startup() { - this.cleanExpireMsgExecutors.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - cleanExpireMsg(); - } - }, clientConfig.getRmqMessageConsumeTimeout(), clientConfig.getRmqMessageConsumeTimeout(), TimeUnit.MINUTES); - } - - @Override - public void shutdown() { - ThreadUtils.shutdownGracefully(cleanExpireMsgExecutors, 5000, TimeUnit.MILLISECONDS); - } - - private void cleanExpireMsg() { - for (final Map.Entry next : rocketmqPullConsumer.getDefaultMQPullConsumerImpl() - .getRebalanceImpl().getProcessQueueTable().entrySet()) { - ProcessQueue pq = next.getValue(); - MessageQueue mq = next.getKey(); - ReadWriteLock lockTreeMap = getLockInProcessQueue(pq); - if (lockTreeMap == null) { - log.error("Gets tree map lock in process queue error, may be has compatibility issue"); - return; - } - - TreeMap msgTreeMap = pq.getMsgTreeMap(); - - int loop = msgTreeMap.size(); - for (int i = 0; i < loop; i++) { - MessageExt msg = null; - try { - lockTreeMap.readLock().lockInterruptibly(); - try { - if (!msgTreeMap.isEmpty()) { - msg = msgTreeMap.firstEntry().getValue(); - if (System.currentTimeMillis() - Long.parseLong(MessageAccessor.getConsumeStartTimeStamp(msg)) - > clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000) { - //Expired, ack and remove it. - } else { - break; - } - } else { - break; - } - } finally { - lockTreeMap.readLock().unlock(); - } - } catch (InterruptedException e) { - log.error("Gets expired message exception", e); - } - - try { - rocketmqPullConsumer.sendMessageBack(msg, 3); - log.info("Send expired msg back. topic={}, msgId={}, storeHost={}, queueId={}, queueOffset={}", - msg.getTopic(), msg.getMsgId(), msg.getStoreHost(), msg.getQueueId(), msg.getQueueOffset()); - ack(mq, pq, msg); - } catch (Exception e) { - log.error("Send back expired msg exception", e); - } - } - } - } - - private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) { - try { - return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true); - } catch (IllegalAccessException e) { - return null; - } - } -} diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PullConsumerImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PullConsumerImpl.java deleted file mode 100644 index c599b4b7aa..0000000000 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PullConsumerImpl.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.webank.eventmesh.connector.rocketmq.consumer; - -import com.webank.eventmesh.connector.rocketmq.domain.ConsumeRequest; -import com.webank.eventmesh.connector.rocketmq.utils.BeanUtils; -import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; -import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.OMSBuiltinKeys; -import io.openmessaging.consumer.PullConsumer; -import io.openmessaging.exception.OMSRuntimeException; -import org.apache.rocketmq.client.consumer.*; -import org.apache.rocketmq.client.exception.MQClientException; -import org.apache.rocketmq.client.impl.consumer.ProcessQueue; -import org.apache.rocketmq.client.log.ClientLogger; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.message.MessageQueue; -import org.apache.rocketmq.logging.InternalLogger; -import org.apache.rocketmq.remoting.protocol.LanguageCode; - -public class PullConsumerImpl implements PullConsumer { - private final DefaultMQPullConsumer rocketmqPullConsumer; - private final KeyValue properties; - private boolean started = false; - private final MQPullConsumerScheduleService pullConsumerScheduleService; - private final LocalMessageCache localMessageCache; - private final ClientConfig clientConfig; - - private final static InternalLogger log = ClientLogger.getLog(); - - 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); - } - - @Override - public KeyValue attributes() { - return properties; - } - - @Override - public PullConsumer attachQueue(String queueName) { - registerPullTaskCallback(queueName); - return this; - } - - @Override - public PullConsumer attachQueue(String queueName, KeyValue attributes) { - registerPullTaskCallback(queueName); - return this; - } - - @Override - public PullConsumer detachQueue(String queueName) { - this.rocketmqPullConsumer.getRegisterTopics().remove(queueName); - return this; - } - - @Override - public Message receive() { - MessageExt rmqMsg = localMessageCache.poll(); - return rmqMsg == null ? null : OMSUtil.msgConvert(rmqMsg); - } - - @Override - public Message receive(final KeyValue properties) { - MessageExt rmqMsg = localMessageCache.poll(properties); - return rmqMsg == null ? null : OMSUtil.msgConvert(rmqMsg); - } - - @Override - public void ack(final String messageId) { - localMessageCache.ack(messageId); - } - - @Override - public void ack(final String messageId, final KeyValue properties) { - localMessageCache.ack(messageId); - } - - @Override - public synchronized void startup() { - if (!started) { - try { - this.pullConsumerScheduleService.start(); - this.localMessageCache.startup(); - } catch (MQClientException e) { - throw new OMSRuntimeException("-1", e); - } - } - this.started = true; - } - - private void registerPullTaskCallback(final String targetQueueName) { - this.pullConsumerScheduleService.registerPullTaskCallback(targetQueueName, new PullTaskCallback() { - @Override - public void doPullTask(final MessageQueue mq, final PullTaskContext context) { - MQPullConsumer consumer = context.getPullConsumer(); - try { - long offset = localMessageCache.nextPullOffset(mq); - - PullResult pullResult = consumer.pull(mq, "*", - offset, localMessageCache.nextPullBatchNums()); - ProcessQueue pq = rocketmqPullConsumer.getDefaultMQPullConsumerImpl().getRebalanceImpl() - .getProcessQueueTable().get(mq); - switch (pullResult.getPullStatus()) { - case FOUND: - if (pq != null) { - pq.putMessage(pullResult.getMsgFoundList()); - for (final MessageExt messageExt : pullResult.getMsgFoundList()) { - localMessageCache.submitConsumeRequest(new ConsumeRequest(messageExt, mq, pq)); - } - } - break; - default: - break; - } - localMessageCache.updatePullOffset(mq, pullResult.getNextBeginOffset()); - } catch (Exception e) { - log.error("An error occurred in pull message process.", e); - } - } - }); - } - - @Override - public synchronized void shutdown() { - if (this.started) { - this.localMessageCache.shutdown(); - this.pullConsumerScheduleService.shutdown(); - this.rocketmqPullConsumer.shutdown(); - } - this.started = false; - } -} diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java index 094ea99361..0deebed84f 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/PushConsumerImpl.java @@ -16,6 +16,7 @@ */ package com.webank.eventmesh.connector.rocketmq.consumer; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.connector.rocketmq.common.ProxyConstants; import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; import com.webank.eventmesh.connector.rocketmq.patch.ProxyConsumeConcurrentlyContext; @@ -23,41 +24,30 @@ import com.webank.eventmesh.connector.rocketmq.patch.ProxyMessageListenerConcurrently; import com.webank.eventmesh.connector.rocketmq.utils.BeanUtils; import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; -import com.webank.eventmesh.connector.rocketmq.utils.ProxyUtil; import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; import com.webank.eventmesh.api.AbstractContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.KeyValue; -import io.openmessaging.OMS; -import io.openmessaging.OMSBuiltinKeys; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.consumer.PushConsumer; -import io.openmessaging.exception.OMSRuntimeException; -import io.openmessaging.interceptor.ConsumerInterceptor; +import io.openmessaging.api.*; +import io.openmessaging.api.exception.OMSRuntimeException; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; -import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; -import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.common.message.MessageExt; import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; import org.apache.rocketmq.remoting.protocol.LanguageCode; -import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; -public class PushConsumerImpl implements PushConsumer { +public class PushConsumerImpl implements Consumer { private final DefaultMQPushConsumer rocketmqPushConsumer; - private final KeyValue properties; - private boolean started = false; - private final Map subscribeTable = new ConcurrentHashMap<>(); + private final Properties properties; + private AtomicBoolean started = new AtomicBoolean(false); + private final Map subscribeTable = new ConcurrentHashMap<>(); private final ClientConfig clientConfig; private ProxyConsumeConcurrentlyContext context; - public PushConsumerImpl(final KeyValue properties) { + public PushConsumerImpl(final Properties properties) { this.rocketmqPushConsumer = new DefaultMQPushConsumer(); this.properties = properties; this.clientConfig = BeanUtils.populate(properties, ClientConfig.class); @@ -68,12 +58,12 @@ public PushConsumerImpl(final KeyValue properties) { // } String accessPoints = clientConfig.getAccessPoints(); if (accessPoints == null || accessPoints.isEmpty()) { - throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty."); + 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."); + throw new OMSRuntimeException(-1, "Consumer Group is necessary for RocketMQ, please set it."); } this.rocketmqPushConsumer.setConsumerGroup(consumerGroup); this.rocketmqPushConsumer.setMaxReconsumeTimes(clientConfig.getRmqMaxRedeliveryTimes()); @@ -84,8 +74,8 @@ public PushConsumerImpl(final KeyValue properties) { String consumerId = OMSUtil.buildInstanceName(); //this.rocketmqPushConsumer.setInstanceName(consumerId); - this.rocketmqPushConsumer.setInstanceName(properties.getString("instanceName")); - properties.put(OMSBuiltinKeys.CONSUMER_ID, consumerId); + this.rocketmqPushConsumer.setInstanceName(properties.getProperty("instanceName")); + properties.put("CONSUMER_ID", consumerId); this.rocketmqPushConsumer.setLanguage(LanguageCode.OMS); if (clientConfig.getMessageModel().equalsIgnoreCase(MessageModel.BROADCASTING.name())){ @@ -94,52 +84,38 @@ public PushConsumerImpl(final KeyValue properties) { @Override public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { PushConsumerImpl.this.setContext(context); - if (msg == null) - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - - if (!ProxyUtil.isValidRMBTopic(msg.getTopic())) { + if (msg == null){ return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; } - msg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); - msg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); - BytesMessage omsMsg = OMSUtil.msgConvert(msg); +// if (!ProxyUtil.isValidRMBTopic(msg.getTopic())) { +// return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } + + msg.putUserProperty(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); + msg.putUserProperty(Constants.PROPERTY_MESSAGE_STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); - MessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); + Message omsMsg = OMSUtil.msgConvert(msg); + + AsyncMessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); if (listener == null) { - throw new OMSRuntimeException("-1", + throw new OMSRuntimeException(-1, String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); } - final KeyValue contextProperties = OMS.newKeyValue(); - final CountDownLatch sync = new CountDownLatch(1); - + final Properties contextProperties = new Properties(); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.RECONSUME_LATER.name()); - - MessageListener.Context omsContext = new MessageListener.Context() { + AsyncConsumeContext omsContext = new AsyncConsumeContext() { @Override - public KeyValue attributes() { - return contextProperties; - } - - @Override - public void ack() { - sync.countDown(); -// contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); + public void commit(Action action) { + contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); } }; - long begin = System.currentTimeMillis(); - listener.onReceived(omsMsg, omsContext); - long costs = System.currentTimeMillis() - begin; - long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; - try { - sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); - } catch (InterruptedException ignore) { - } + listener.consume(omsMsg, omsContext); - return ProxyConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); + return ProxyConsumeConcurrentlyStatus.valueOf(contextProperties.getProperty(NonStandardKeys.MESSAGE_CONSUME_STATUS)); } }); }else { @@ -148,138 +124,74 @@ public void ack() { @Override public ProxyConsumeConcurrentlyStatus handleMessage(MessageExt msg, ProxyConsumeConcurrentlyContext context) { PushConsumerImpl.this.setContext(context); - if (msg == null) - return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; - - if (!ProxyUtil.isValidRMBTopic(msg.getTopic())) { + if (msg == null) { return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; } +// if (!ProxyUtil.isValidRMBTopic(msg.getTopic())) { +// return ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS; +// } - msg.putUserProperty(ProxyConstants.BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); + msg.putUserProperty(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, String.valueOf(msg.getBornTimestamp())); msg.putUserProperty(ProxyConstants.STORE_TIMESTAMP, String.valueOf(msg.getStoreTimestamp())); - BytesMessage omsMsg = OMSUtil.msgConvert(msg); + Message omsMsg = OMSUtil.msgConvert(msg); - MessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); + AsyncMessageListener listener = PushConsumerImpl.this.subscribeTable.get(msg.getTopic()); if (listener == null) { - throw new OMSRuntimeException("-1", + throw new OMSRuntimeException(-1, String.format("The topic/queue %s isn't attached to this consumer", msg.getTopic())); } - final KeyValue contextProperties = OMS.newKeyValue(); - final CountDownLatch sync = new CountDownLatch(1); + final Properties contextProperties = new Properties(); contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.RECONSUME_LATER.name()); - MessageListener.Context omsContext = new MessageListener.Context() { + AsyncConsumeContext omsContext = new AsyncConsumeContext() { @Override - public KeyValue attributes() { - return contextProperties; - } - - @Override - public void ack() { - sync.countDown(); + public void commit(Action action) { contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); } }; - long begin = System.currentTimeMillis(); - listener.onReceived(omsMsg, omsContext); - long costs = System.currentTimeMillis() - begin; - long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000; - try { - sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS); - } catch (InterruptedException ignore) { - } + listener.consume(omsMsg, omsContext); - return ProxyConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS)); + return ProxyConsumeConcurrentlyStatus.valueOf(contextProperties.getProperty(NonStandardKeys.MESSAGE_CONSUME_STATUS)); } }); } } - @Override - public KeyValue attributes() { + public Properties attributes() { return properties; } @Override - public void resume() { - this.rocketmqPushConsumer.resume(); - } - - @Override - public void suspend() { - this.rocketmqPushConsumer.suspend(); - } - - @Override - public void suspend(long timeout) { - - } - - @Override - public boolean isSuspended() { - return this.rocketmqPushConsumer.getDefaultMQPushConsumerImpl().isPause(); - } - - @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)); + public void start() { + if (this.started.compareAndSet(false, true)) { + try { + this.rocketmqPushConsumer.start(); + } catch (Exception e) { + throw new OMSRuntimeException(e.getMessage()); + } } - return this; - } - - @Override - public PushConsumer attachQueue(String queueName, MessageListener listener, KeyValue attributes) { - return this.attachQueue(queueName, listener); } @Override - public PushConsumer detachQueue(String queueName) { - this.subscribeTable.remove(queueName); - try { - this.rocketmqPushConsumer.unsubscribe(queueName); - } catch (Exception e) { - throw new OMSRuntimeException("-1", String.format("RocketMQ push consumer fails to unsubscribe topic: %s", queueName)); + public synchronized void shutdown() { + if (this.started.compareAndSet(true, false)) { + this.rocketmqPushConsumer.shutdown(); } - return null; - } - - @Override - public void addInterceptor(ConsumerInterceptor interceptor) { - - } - - @Override - public void removeInterceptor(ConsumerInterceptor interceptor) { - } @Override - public synchronized void startup() { - if (!started) { - try { - this.rocketmqPushConsumer.start(); - } catch (MQClientException e) { - throw new OMSRuntimeException("-1", e); - } - } - this.started = true; + public boolean isStarted() { + return this.started.get(); } @Override - public synchronized void shutdown() { - if (this.started) { - this.rocketmqPushConsumer.shutdown(); - } - this.started = false; + public boolean isClosed() { + return !this.isStarted(); } public DefaultMQPushConsumer getRocketmqPushConsumer() { @@ -339,4 +251,64 @@ public AbstractContext getContext() { public void setContext(ProxyConsumeConcurrentlyContext context) { this.context = context; } + + @Override + public void subscribe(String topic, String subExpression, MessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, MessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, GenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, GenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, AsyncMessageListener listener) { + this.subscribeTable.put(topic, listener); + try { + this.rocketmqPushConsumer.subscribe(topic, subExpression); + } catch (MQClientException e) { + throw new OMSRuntimeException(-1, String.format("RocketMQ push consumer can't attach to %s.", topic)); + } + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncMessageListener listener) { + + } + + @Override + public void subscribe(String topic, String subExpression, AsyncGenericMessageListener listener) { + + } + + @Override + public void subscribe(String topic, MessageSelector selector, AsyncGenericMessageListener listener) { + + } + + @Override + public void unsubscribe(String topic) { + this.subscribeTable.remove(topic); + try { + this.rocketmqPushConsumer.unsubscribe(topic); + } catch (Exception e) { + throw new OMSRuntimeException(-1, String.format("RocketMQ push consumer fails to unsubscribe topic: %s", topic)); + } + } + + @Override + public void updateCredential(Properties credentialProperties) { + + } } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java index 1b7ac6097b..0229ac8a91 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/consumer/RocketMQConsumerImpl.java @@ -25,10 +25,7 @@ import com.webank.eventmesh.connector.rocketmq.config.ConfigurationWraper; import com.webank.eventmesh.connector.rocketmq.patch.ProxyConsumeConcurrentlyContext; import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; -import io.openmessaging.*; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.consumer.PushConsumer; -import io.openmessaging.interceptor.ConsumerInterceptor; +import io.openmessaging.api.*; import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService; import org.apache.rocketmq.client.impl.consumer.ConsumeMessageService; import org.apache.rocketmq.common.message.MessageExt; @@ -39,6 +36,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class RocketMQConsumerImpl implements MeshMQPushConsumer { @@ -51,16 +49,17 @@ public class RocketMQConsumerImpl implements MeshMQPushConsumer { private PushConsumerImpl pushConsumer; @Override - public synchronized void init(KeyValue keyValue) throws Exception { + public synchronized void init(Properties keyValue) throws Exception { ConfigurationWraper configurationWraper = new ConfigurationWraper(ProxyConstants.PROXY_CONF_HOME + File.separator + ProxyConstants.PROXY_CONF_FILE, false); final ClientConfiguration clientConfiguration = new ClientConfiguration(configurationWraper); clientConfiguration.init(); - boolean isBroadcast = Boolean.valueOf(keyValue.getString("isBroadcast")); - String consumerGroup = keyValue.getString("consumerGroup"); - String instanceName = keyValue.getString("instanceName"); + boolean isBroadcast = Boolean.parseBoolean(keyValue.getProperty("isBroadcast")); + String consumerGroup = keyValue.getProperty("consumerGroup"); + String instanceName = keyValue.getProperty("instanceName"); + if(isBroadcast){ consumerGroup = Constants.CONSUMER_GROUP_NAME_PREFIX + Constants.BROADCAST_PREFIX + consumerGroup; @@ -68,26 +67,26 @@ public synchronized void init(KeyValue keyValue) throws Exception { consumerGroup = Constants.CONSUMER_GROUP_NAME_PREFIX + consumerGroup; } - String omsNamesrv = "oms:rocketmq://" + clientConfiguration.namesrvAddr + "/namespace"; - KeyValue properties = OMS.newKeyValue().put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); - - properties.put("ACCESS_POINTS", omsNamesrv) - .put("REGION", "namespace") - .put(OMSBuiltinKeys.CONSUMER_ID, consumerGroup) - .put("instanceName", instanceName); + String omsNamesrv = clientConfiguration.namesrvAddr; +// KeyValue properties = OMS.newKeyValue().put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + Properties properties = new Properties(); + properties.put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + properties.put("ACCESS_POINTS", omsNamesrv); + properties.put("REGION", "namespace"); + properties.put("instanceName", instanceName); + properties.put("CONSUMER_ID", consumerGroup); if (isBroadcast){ properties.put("MESSAGE_MODEL", MessageModel.BROADCASTING.name()); }else { properties.put("MESSAGE_MODEL", MessageModel.CLUSTERING.name()); } - - MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(omsNamesrv, properties); - pushConsumer = (PushConsumerImpl)messagingAccessPoint.createPushConsumer(); + MessagingAccessPoint messagingAccessPoint = OMS.builder().build(properties); + pushConsumer = (PushConsumerImpl)messagingAccessPoint.createConsumer(properties); } @Override - public void subscribe(String topic, MessageListener listener) throws Exception { - pushConsumer.attachQueue(topic, listener); + public void subscribe(String topic, AsyncMessageListener listener) throws Exception { + pushConsumer.subscribe(topic, "*", listener); } @Override @@ -97,8 +96,18 @@ public AbstractContext getContext() { @Override - public synchronized void start() throws Exception { - pushConsumer.startup(); + public boolean isStarted() { + return pushConsumer.isStarted(); + } + + @Override + public boolean isClosed() { + return pushConsumer.isClosed(); + } + + @Override + public synchronized void start() { + pushConsumer.start(); } @Override @@ -112,23 +121,8 @@ public void updateOffset(List msgs, AbstractContext context) { } @Override - public void unsubscribe(String topic) throws Exception { - pushConsumer.detachQueue(topic); - } - - @Override - public boolean isPause() { - return pushConsumer.isSuspended(); - } - - @Override - public void pause() { - pushConsumer.suspend(); - } - - @Override - public void startup() { - pushConsumer.startup(); + public void unsubscribe(String topic) { + pushConsumer.unsubscribe(topic); } @Override @@ -136,53 +130,52 @@ public synchronized void shutdown() { pushConsumer.shutdown(); } - @Override - public KeyValue attributes() { + public Properties attributes() { return pushConsumer.attributes(); } @Override - public void resume() { - pushConsumer.resume(); + public void subscribe(String topic, String subExpression, MessageListener listener) { + } @Override - public void suspend() { - pushConsumer.suspend(); + public void subscribe(String topic, MessageSelector selector, MessageListener listener) { + } @Override - public void suspend(long timeout) { - pushConsumer.suspend(timeout); + public void subscribe(String topic, String subExpression, GenericMessageListener listener) { + } @Override - public boolean isSuspended() { - return pushConsumer.isSuspended(); + public void subscribe(String topic, MessageSelector selector, GenericMessageListener listener) { + } @Override - public PushConsumer attachQueue(String queueName, MessageListener listener) { - return pushConsumer.attachQueue(queueName, listener); + public void subscribe(String topic, String subExpression, AsyncMessageListener listener) { + } @Override - public PushConsumer attachQueue(String queueName, MessageListener listener, KeyValue attributes) { - return pushConsumer.attachQueue(queueName, listener, attributes); + public void subscribe(String topic, MessageSelector selector, AsyncMessageListener listener) { + } @Override - public PushConsumer detachQueue(String queueName) { - return pushConsumer.detachQueue(queueName); + public void subscribe(String topic, String subExpression, AsyncGenericMessageListener listener) { + } @Override - public void addInterceptor(ConsumerInterceptor interceptor) { - pushConsumer.addInterceptor(interceptor); + public void subscribe(String topic, MessageSelector selector, AsyncGenericMessageListener listener) { + } @Override - public void removeInterceptor(ConsumerInterceptor interceptor) { - pushConsumer.removeInterceptor(interceptor); + public void updateCredential(Properties credentialProperties) { + } } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/domain/SendResultImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/domain/SendResultImpl.java deleted file mode 100644 index 2dddc02e57..0000000000 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/domain/SendResultImpl.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.webank.eventmesh.connector.rocketmq.domain; - -import io.openmessaging.KeyValue; -import io.openmessaging.producer.SendResult; - -public class SendResultImpl implements SendResult { - private String messageId; - private KeyValue properties; - - public SendResultImpl(final String messageId, final KeyValue properties) { - this.messageId = messageId; - this.properties = properties; - } - - @Override - public String messageId() { - return messageId; - } - - public KeyValue properties() { - return properties; - } -} diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java index 30d6d7c29c..b47587f44a 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/AbstractOMSProducer.java @@ -19,14 +19,12 @@ import com.webank.eventmesh.connector.rocketmq.utils.BeanUtils; import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; -import com.webank.eventmesh.connector.rocketmq.domain.BytesMessageImpl; -import io.openmessaging.*; -import io.openmessaging.exception.OMSMessageFormatException; -import io.openmessaging.exception.OMSNotSupportedException; -import io.openmessaging.exception.OMSRuntimeException; -import io.openmessaging.exception.OMSTimeOutException; +import io.openmessaging.api.exception.OMSRuntimeException; +import io.openmessaging.api.exception.OMSMessageFormatException; +import io.openmessaging.api.exception.OMSTimeOutException; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl; import org.apache.rocketmq.client.log.ClientLogger; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.common.protocol.ResponseCode; @@ -35,24 +33,27 @@ import org.apache.rocketmq.remoting.exception.RemotingTimeoutException; import org.apache.rocketmq.remoting.protocol.LanguageCode; -public abstract class AbstractOMSProducer implements ServiceLifecycle, MessageFactory { +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; + +public abstract class AbstractOMSProducer { + final static InternalLogger log = ClientLogger.getLog(); - final KeyValue properties; + final Properties properties; final DefaultMQProducer rocketmqProducer; - private boolean started = false; + protected final AtomicBoolean started = new AtomicBoolean(false); +// private boolean started = false; private final ClientConfig clientConfig; + private final String PRODUCER_ID = "PRODUCER_ID"; - AbstractOMSProducer(final KeyValue properties) { + AbstractOMSProducer(final Properties 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."); + throw new OMSRuntimeException(-1, "OMS AccessPoints is null or empty."); } this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';')); @@ -64,45 +65,51 @@ public abstract class AbstractOMSProducer implements ServiceLifecycle, MessageFa this.rocketmqProducer.setInstanceName(producerId); this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4); this.rocketmqProducer.setLanguage(LanguageCode.OMS); - properties.put(OMSBuiltinKeys.PRODUCER_ID, producerId); + properties.put(PRODUCER_ID, producerId); } - @Override - public synchronized void startup() { - if (!started) { + public synchronized void start() { + if (!started.get()) { try { this.rocketmqProducer.start(); } catch (MQClientException e) { throw new OMSRuntimeException("-1", e); } } - this.started = true; + this.started.set(true); } - @Override public synchronized void shutdown() { - if (this.started) { + if (this.started.get()) { this.rocketmqProducer.shutdown(); } - this.started = false; + this.started.set(false); + } + + public boolean isStarted() { + return this.started.get(); + } + + public boolean isClosed() { + return !this.isStarted(); } OMSRuntimeException checkProducerException(String topic, String msgId, Throwable e) { if (e instanceof MQClientException) { if (e.getCause() != null) { if (e.getCause() instanceof RemotingTimeoutException) { - return new OMSTimeOutException("-1", String.format("Send message to broker timeout, %dms, Topic=%s, msgId=%s", + return new OMSTimeOutException(-1, String.format("Send message to broker timeout, %dms, Topic=%s, msgId=%s", this.rocketmqProducer.getSendMsgTimeout(), topic, msgId), e); } else if (e.getCause() instanceof MQBrokerException || e.getCause() instanceof RemotingConnectException) { if (e.getCause() instanceof MQBrokerException) { MQBrokerException brokerException = (MQBrokerException) e.getCause(); - return new OMSRuntimeException("-1", String.format("Received a broker exception, Topic=%s, msgId=%s, %s", + return new OMSRuntimeException(-1, String.format("Received a broker exception, Topic=%s, msgId=%s, %s", topic, msgId, brokerException.getErrorMessage()), e); } if (e.getCause() instanceof RemotingConnectException) { RemotingConnectException connectException = (RemotingConnectException)e.getCause(); - return new OMSRuntimeException("-1", + return new OMSRuntimeException(-1, String.format("Network connection experiences failures. Topic=%s, msgId=%s, %s", topic, msgId, connectException.getMessage()), e); @@ -113,31 +120,30 @@ OMSRuntimeException checkProducerException(String topic, String msgId, Throwable else { MQClientException clientException = (MQClientException) e; if (-1 == clientException.getResponseCode()) { - return new OMSRuntimeException("-1", String.format("Topic does not exist, Topic=%s, msgId=%s", + return new OMSRuntimeException(-1, String.format("Topic does not exist, Topic=%s, msgId=%s", topic, msgId), e); } else if (ResponseCode.MESSAGE_ILLEGAL == clientException.getResponseCode()) { - return new OMSMessageFormatException("-1", String.format("A illegal message for RocketMQ, Topic=%s, msgId=%s", + return new OMSMessageFormatException(-1, String.format("A illegal message for RocketMQ, Topic=%s, msgId=%s", topic, msgId), e); } } } - return new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.", e); + return new OMSRuntimeException(-1, "Send message to RocketMQ broker failed.", e); } - protected void checkMessageType(Message message) { - if (!(message instanceof BytesMessage)) { - throw new OMSNotSupportedException("-1", "Only BytesMessage is supported."); + protected void checkProducerServiceState(DefaultMQProducerImpl producer) { + switch(producer.getServiceState()) { + case CREATE_JUST: + throw new OMSRuntimeException(String.format("You do not have start the producer, %s", producer.getServiceState())); + case SHUTDOWN_ALREADY: + throw new OMSRuntimeException(String.format("Your producer has been shut down, %s", producer.getServiceState())); + case START_FAILED: + throw new OMSRuntimeException(String.format("When you start your service throws an exception, %s", producer.getServiceState())); + case RUNNING: + default: } } - @Override - public BytesMessage createBytesMessage(String queue, byte[] body) { - BytesMessage message = new BytesMessageImpl(); - message.setBody(body); - message.sysHeaders().put(Message.BuiltinKeys.DESTINATION, queue); - return message; - } - public DefaultMQProducer getRocketmqProducer() { return rocketmqProducer; } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/ProducerImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/ProducerImpl.java index f9cef8be62..c8577264c8 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/ProducerImpl.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/ProducerImpl.java @@ -16,146 +16,224 @@ */ package com.webank.eventmesh.connector.rocketmq.producer; -import com.webank.eventmesh.connector.rocketmq.promise.DefaultPromise; import com.webank.eventmesh.connector.rocketmq.utils.OMSUtil; -import com.webank.eventmesh.api.SendCallback; -import io.openmessaging.*; -import io.openmessaging.exception.OMSRuntimeException; -import io.openmessaging.interceptor.ProducerInterceptor; -import io.openmessaging.producer.BatchMessageSender; -import io.openmessaging.producer.LocalTransactionExecutor; -import io.openmessaging.producer.Producer; -import io.openmessaging.producer.SendResult; -import org.apache.rocketmq.client.producer.SendStatus; +import io.openmessaging.api.*; +import io.openmessaging.api.exception.OMSRuntimeException; +import org.apache.rocketmq.common.message.MessageClientIDSetter; + +import java.util.Properties; +import java.util.concurrent.ExecutorService; -import static com.webank.eventmesh.connector.rocketmq.utils.OMSUtil.msgConvert; public class ProducerImpl extends AbstractOMSProducer implements Producer { public static final int proxyServerAsyncAccumulationThreshold = 1000; - public ProducerImpl(final KeyValue properties) { + public ProducerImpl(final Properties properties) { super(properties); } - @Override - public KeyValue attributes() { + public Properties attributes() { return properties; } - @Override - public SendResult send(final Message message) { - return send(message, this.rocketmqProducer.getSendMsgTimeout()); - } +// @Override +// public SendResult send(final Message message) { +// return send(message, this.rocketmqProducer.getSendMsgTimeout()); +// } + +// @Override +// public SendResult send(final Message message, final KeyValue properties) { +// long timeout = properties.containsKey(Message.BuiltinKeys.TIMEOUT) +// ? properties.getInt(Message.BuiltinKeys.TIMEOUT) : this.rocketmqProducer.getSendMsgTimeout(); +// return send(message, timeout); +// } + +// @Override +// public SendResult send(Message message, LocalTransactionExecutor branchExecutor, KeyValue attributes) { +// return null; +// } + +// @Override +// public Future sendAsync(Message message) { +// return null; +// } +// +// @Override +// public Future sendAsync(Message message, KeyValue attributes) { +// return null; +// } +// +// private SendResult send(final Message message, long timeout) { +// checkMessageType(message); +// org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); +// try { +// org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout); +// if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) { +// log.error(String.format("Send message to RocketMQ failed, %s", message)); +// throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed."); +// } +// message.sysHeaders().put(Message.BuiltinKeys.MESSAGE_ID, rmqResult.getMsgId()); +// return OMSUtil.sendResultConvert(rmqResult); +// } catch (Exception e) { +// log.error(String.format("Send message to RocketMQ failed, %s", message), e); +// throw checkProducerException(rmqMessage.getTopic(), message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID), e); +// } +// } +// +// public Promise sendAsync(final Message message, SendCallback sendCallback) { +// return sendAsync(message, this.rocketmqProducer.getSendMsgTimeout(), sendCallback); +// } +// +// private Promise sendAsync(final Message message, long timeout, SendCallback sendCallback) { +// checkMessageType(message); +// org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); +// final Promise promise = new DefaultPromise<>(); +// try { +// this.rocketmqProducer.send(rmqMessage, new org.apache.rocketmq.client.producer.SendCallback() { +// @Override +// public void onSuccess(final org.apache.rocketmq.client.producer.SendResult rmqResult) { +// message.sysHeaders().put(Message.BuiltinKeys.MESSAGE_ID, rmqResult.getMsgId()); +// SendResult omsSendResult = OMSUtil.sendResultConvert(rmqResult); +// promise.set(omsSendResult); +// sendCallback.onSuccess(omsSendResult); +// } +// +// @Override +// public void onException(final Throwable e) { +// promise.setFailure(e); +// sendCallback.onException(e); +// } +// }, timeout); +// } catch (Exception e) { +// promise.setFailure(e); +// } +// return promise; +// } +// +// @Override +// public void sendOneway(final Message message) { +// checkMessageType(message); +// org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); +// try { +// this.rocketmqProducer.sendOneway(rmqMessage); +// } catch (Exception ignore) { //Ignore the oneway exception. +// } +// } + + + +// @Override +// public void sendOneway(final Message message, final KeyValue properties) { +// sendOneway(message); +// } +// +// @Override +// public BatchMessageSender createBatchMessageSender() { +// return null; +// } +// +// @Override +// public void addInterceptor(ProducerInterceptor interceptor) { +// +// } +// +// @Override +// public void removeInterceptor(ProducerInterceptor interceptor) { +// +// } - @Override - public SendResult send(final Message message, final KeyValue properties) { - long timeout = properties.containsKey(Message.BuiltinKeys.TIMEOUT) - ? properties.getInt(Message.BuiltinKeys.TIMEOUT) : this.rocketmqProducer.getSendMsgTimeout(); - return send(message, timeout); - } - - @Override - public SendResult send(Message message, LocalTransactionExecutor branchExecutor, KeyValue attributes) { - return null; - } + public void setExtFields(){ + super.getRocketmqProducer().setRetryTimesWhenSendFailed(0); + super.getRocketmqProducer().setRetryTimesWhenSendAsyncFailed(0); + super.getRocketmqProducer().setPollNameServerInterval(60000); - @Override - public Future sendAsync(Message message) { - return null; + super.getRocketmqProducer().getDefaultMQProducerImpl().getmQClientFactory() + .getNettyClientConfig().setClientAsyncSemaphoreValue(proxyServerAsyncAccumulationThreshold); + super.getRocketmqProducer().setCompressMsgBodyOverHowmuch(10); } @Override - public Future sendAsync(Message message, KeyValue attributes) { - return null; - } + public SendResult send(Message message) { + this.checkProducerServiceState(rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); - private SendResult send(final Message message, long timeout) { - checkMessageType(message); - org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); try { - org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout); - if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) { - log.error(String.format("Send message to RocketMQ failed, %s", message)); - throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed."); - } - message.sysHeaders().put(Message.BuiltinKeys.MESSAGE_ID, rmqResult.getMsgId()); - return OMSUtil.sendResultConvert(rmqResult); + org.apache.rocketmq.client.producer.SendResult sendResultRMQ = this.rocketmqProducer.send(msgRMQ); + message.setMsgID(sendResultRMQ.getMsgId()); + SendResult sendResult = new SendResult(); + sendResult.setTopic(sendResultRMQ.getMessageQueue().getTopic()); + sendResult.setMessageId(sendResultRMQ.getMsgId()); + return sendResult; } catch (Exception e) { - log.error(String.format("Send message to RocketMQ failed, %s", message), e); - throw checkProducerException(rmqMessage.getTopic(), message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID), e); + log.error(String.format("Send message Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); } } - public Promise sendAsync(final Message message, SendCallback sendCallback) { - return sendAsync(message, this.rocketmqProducer.getSendMsgTimeout(), sendCallback); - } + @Override + public void sendOneway(Message message) { + this.checkProducerServiceState(this.rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); - private Promise sendAsync(final Message message, long timeout, SendCallback sendCallback) { - checkMessageType(message); - org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); - final Promise promise = new DefaultPromise<>(); try { - this.rocketmqProducer.send(rmqMessage, new org.apache.rocketmq.client.producer.SendCallback() { - @Override - public void onSuccess(final org.apache.rocketmq.client.producer.SendResult rmqResult) { - message.sysHeaders().put(Message.BuiltinKeys.MESSAGE_ID, rmqResult.getMsgId()); - SendResult omsSendResult = OMSUtil.sendResultConvert(rmqResult); - promise.set(omsSendResult); - sendCallback.onSuccess(omsSendResult); - } - - @Override - public void onException(final Throwable e) { - promise.setFailure(e); - sendCallback.onException(e); - } - }, timeout); + this.rocketmqProducer.sendOneway(msgRMQ); + message.setMsgID(MessageClientIDSetter.getUniqID(msgRMQ)); } catch (Exception e) { - promise.setFailure(e); + log.error(String.format("Send message oneway Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); } - return promise; } @Override - public void sendOneway(final Message message) { - checkMessageType(message); - org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message); + public void sendAsync(Message message, SendCallback sendCallback) { + this.checkProducerServiceState(this.rocketmqProducer.getDefaultMQProducerImpl()); + org.apache.rocketmq.common.message.Message msgRMQ = OMSUtil.msgConvert(message); + try { - this.rocketmqProducer.sendOneway(rmqMessage); - } catch (Exception ignore) { //Ignore the oneway exception. + this.rocketmqProducer.send(msgRMQ, this.sendCallbackConvert(message, sendCallback)); + message.setMsgID(MessageClientIDSetter.getUniqID(msgRMQ)); + } catch (Exception e) { + log.error(String.format("Send message async Exception, %s", message), e); + throw this.checkProducerException(message.getTopic(), message.getMsgID(), e); } } - @Override - public void sendOneway(final Message message, final KeyValue properties) { - sendOneway(message); - } + private org.apache.rocketmq.client.producer.SendCallback sendCallbackConvert(final Message message, final SendCallback sendCallback) { + org.apache.rocketmq.client.producer.SendCallback rmqSendCallback = new org.apache.rocketmq.client.producer.SendCallback() { + @Override + public void onSuccess(org.apache.rocketmq.client.producer.SendResult sendResult) { + sendCallback.onSuccess(OMSUtil.sendResultConvert(sendResult)); + } - @Override - public BatchMessageSender createBatchMessageSender() { - return null; + @Override + public void onException(Throwable e) { + String topic = message.getTopic(); + String msgId = message.getMsgID(); + OMSRuntimeException onsEx = ProducerImpl.this.checkProducerException(topic, msgId, e); + OnExceptionContext context = new OnExceptionContext(); + context.setTopic(topic); + context.setMessageId(msgId); + context.setException(onsEx); + sendCallback.onException(context); + } + }; + return rmqSendCallback; } @Override - public void addInterceptor(ProducerInterceptor interceptor) { - + public void setCallbackExecutor(ExecutorService callbackExecutor) { +// this.rocketmqProducer.setCallbackExecutor(callbackExecutor); } @Override - public void removeInterceptor(ProducerInterceptor interceptor) { + public void updateCredential(Properties credentialProperties) { } - public void setExtFields(){ - super.getRocketmqProducer().setRetryTimesWhenSendFailed(0); - super.getRocketmqProducer().setRetryTimesWhenSendAsyncFailed(0); - super.getRocketmqProducer().setPollNameServerInterval(60000); - - super.getRocketmqProducer().getDefaultMQProducerImpl().getmQClientFactory() - .getNettyClientConfig().setClientAsyncSemaphoreValue(proxyServerAsyncAccumulationThreshold); - super.getRocketmqProducer().setCompressMsgBodyOverHowmuch(10); + @Override + public MessageBuilder messageBuilder() { + return null; } - } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java index bd3b5df010..3e022b1624 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/producer/RocketMQProducerImpl.java @@ -18,16 +18,11 @@ package com.webank.eventmesh.connector.rocketmq.producer; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.api.producer.MeshMQProducer; import com.webank.eventmesh.connector.rocketmq.common.ProxyConstants; import com.webank.eventmesh.connector.rocketmq.config.ClientConfiguration; import com.webank.eventmesh.connector.rocketmq.config.ConfigurationWraper; -import io.openmessaging.*; -import io.openmessaging.interceptor.ProducerInterceptor; -import io.openmessaging.producer.BatchMessageSender; -import io.openmessaging.producer.LocalTransactionExecutor; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.*; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.remoting.exception.RemotingException; @@ -35,46 +30,57 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.util.Properties; +import java.util.concurrent.ExecutorService; public class RocketMQProducerImpl implements MeshMQProducer { public Logger logger = LoggerFactory.getLogger(this.getClass()); private ProducerImpl producer; + public final String DEFAULT_ACCESS_DRIVER = "com.webank.eventmesh.connector.rocketmq.MessagingAccessPointImpl"; @Override - public synchronized void init(KeyValue keyValue) { + public synchronized void init(Properties keyValue) { ConfigurationWraper configurationWraper = new ConfigurationWraper(ProxyConstants.PROXY_CONF_HOME + File.separator + ProxyConstants.PROXY_CONF_FILE, false); final ClientConfiguration clientConfiguration = new ClientConfiguration(configurationWraper); clientConfiguration.init(); - String producerGroup = keyValue.getString("producerGroup"); + String producerGroup = keyValue.getProperty("producerGroup"); + + String omsNamesrv = clientConfiguration.namesrvAddr; + Properties properties = new Properties(); + properties.put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); + properties.put("ACCESS_POINTS", omsNamesrv); + properties.put("REGION", "namespace"); + properties.put("RMQ_PRODUCER_GROUP", producerGroup); + properties.put("OPERATION_TIMEOUT", 3000); + properties.put("PRODUCER_ID", producerGroup); - String omsNamesrv = "oms:rocketmq://" + clientConfiguration.namesrvAddr + "/namespace"; - KeyValue properties = OMS.newKeyValue().put(OMSBuiltinKeys.DRIVER_IMPL, DEFAULT_ACCESS_DRIVER); - properties.put("ACCESS_POINTS", omsNamesrv) - .put("REGION", "namespace") - .put(OMSBuiltinKeys.PRODUCER_ID, producerGroup) - .put("RMQ_PRODUCER_GROUP", producerGroup) - .put(OMSBuiltinKeys.OPERATION_TIMEOUT, 3000); - MessagingAccessPoint messagingAccessPoint = OMS.getMessagingAccessPoint(omsNamesrv, properties); - producer = (ProducerImpl) messagingAccessPoint.createProducer(); + MessagingAccessPoint messagingAccessPoint = OMS.builder().build(properties); + producer = (ProducerImpl) messagingAccessPoint.createProducer(properties); } @Override - public synchronized void start() throws Exception { - producer.startup(); + public boolean isStarted() { + return producer.isStarted(); } @Override - public void startup() { - producer.startup(); + public boolean isClosed() { + return producer.isClosed(); } + @Override + public void start() { + producer.start(); + } + + @Override public synchronized void shutdown() { producer.shutdown(); @@ -124,62 +130,32 @@ public void getDefaultTopicRouteInfoFromNameServer(String topic, long timeout) t } @Override - public KeyValue attributes() { - return producer.attributes(); - } - - @Override - public SendResult send(io.openmessaging.Message message) { + public SendResult send(Message message) { return producer.send(message); } @Override - public SendResult send(io.openmessaging.Message message, KeyValue attributes) { - return producer.send(message, attributes); - } - - @Override - public SendResult send(io.openmessaging.Message message, LocalTransactionExecutor branchExecutor, KeyValue attributes) { - return producer.send(message, branchExecutor, attributes); - } - - @Override - public Future sendAsync(io.openmessaging.Message message) { - return producer.sendAsync(message); - } - - @Override - public Future sendAsync(io.openmessaging.Message message, KeyValue attributes) { - return producer.sendAsync(message, attributes); - } - - @Override - public void sendOneway(io.openmessaging.Message message) { + public void sendOneway(Message message) { producer.sendOneway(message); } @Override - public void sendOneway(io.openmessaging.Message message, KeyValue properties) { - producer.sendOneway(message, properties); - } - - @Override - public BatchMessageSender createBatchMessageSender() { - return producer.createBatchMessageSender(); + public void sendAsync(Message message, SendCallback sendCallback) { + producer.sendAsync(message, sendCallback); } @Override - public void addInterceptor(ProducerInterceptor interceptor) { - producer.addInterceptor(interceptor); + public void setCallbackExecutor(ExecutorService callbackExecutor) { + producer.setCallbackExecutor(callbackExecutor); } @Override - public void removeInterceptor(ProducerInterceptor interceptor) { - producer.removeInterceptor(interceptor); + public void updateCredential(Properties credentialProperties) { + producer.updateCredential(credentialProperties); } @Override - public BytesMessage createBytesMessage(String queue, byte[] body) { - return producer.createBytesMessage(queue, body); + public MessageBuilder messageBuilder() { + return null; } } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/promise/DefaultPromise.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/promise/DefaultPromise.java index 01d93979cc..8469db4705 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/promise/DefaultPromise.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/promise/DefaultPromise.java @@ -16,16 +16,16 @@ */ package com.webank.eventmesh.connector.rocketmq.promise; -import io.openmessaging.FutureListener; -import io.openmessaging.Promise; -import io.openmessaging.exception.OMSRuntimeException; +import io.openmessaging.api.Future; +import io.openmessaging.api.FutureListener; +import io.openmessaging.api.exception.OMSRuntimeException; import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLoggerFactory; import java.util.ArrayList; import java.util.List; -public class DefaultPromise implements Promise { +public class DefaultPromise implements Future { private static final InternalLogger LOG = InternalLoggerFactory.getLogger(DefaultPromise.class); private final Object lock = new Object(); private volatile FutureState state = FutureState.DOING; @@ -104,18 +104,18 @@ public V get(final long timeout) { } } - @Override public boolean set(final V value) { - if (value == null) + if (value == null){ return false; + } this.result = value; return done(); } - @Override public boolean setFailure(final Throwable cause) { - if (cause == null) + if (cause == null){ return false; + } this.exception = cause; return done(); } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/BeanUtils.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/BeanUtils.java index 2e527aa1e9..c624ee19cb 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/BeanUtils.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/BeanUtils.java @@ -16,7 +16,6 @@ */ package com.webank.eventmesh.connector.rocketmq.utils; -import io.openmessaging.KeyValue; import org.apache.commons.lang3.StringUtils; import org.apache.rocketmq.client.log.ClientLogger; import org.apache.rocketmq.logging.InternalLogger; @@ -95,16 +94,16 @@ public static T populate(final Properties properties, final Class clazz) return obj; } - public static T populate(final KeyValue properties, final Class clazz) { - T obj = null; - try { - obj = clazz.newInstance(); - return populate(properties, obj); - } catch (Throwable e) { - log.warn("Error occurs !", e); - } - return obj; - } +// public static T populate(final KeyValue properties, final Class clazz) { +// T obj = null; +// try { +// obj = clazz.newInstance(); +// return populate(properties, obj); +// } catch (Throwable e) { +// log.warn("Error occurs !", e); +// } +// return obj; +// } public static Class getMethodClass(Class clazz, String methodName) { Method[] methods = clazz.getMethods(); @@ -142,7 +141,7 @@ public static T populate(final Properties properties, final T obj) { Set> entries = properties.entrySet(); for (Map.Entry entry : entries) { String entryKey = entry.getKey().toString(); - String[] keyGroup = entryKey.split("\\."); + String[] keyGroup = entryKey.split("[\\._]"); for (int i = 0; i < keyGroup.length; i++) { keyGroup[i] = keyGroup[i].toLowerCase(); keyGroup[i] = StringUtils.capitalize(keyGroup[i]); @@ -160,28 +159,28 @@ public static T populate(final Properties properties, final T obj) { return obj; } - public static T populate(final KeyValue properties, final T obj) { - Class clazz = obj.getClass(); - try { - - final Set keySet = properties.keySet(); - for (String key : keySet) { - String[] keyGroup = key.split("[\\._]"); - for (int i = 0; i < keyGroup.length; i++) { - keyGroup[i] = keyGroup[i].toLowerCase(); - keyGroup[i] = StringUtils.capitalize(keyGroup[i]); - } - String beanFieldNameWithCapitalization = StringUtils.join(keyGroup); - try { - setProperties(clazz, obj, "set" + beanFieldNameWithCapitalization, properties.getString(key)); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { - //ignored... - } - } - } catch (RuntimeException e) { - log.warn("Error occurs !", e); - } - return obj; - } +// public static T populate(final Properties properties, final T obj) { +// Class clazz = obj.getClass(); +// try { +// +// final Set keySet = properties.keySet(); +// for (Object key : keySet) { +// String[] keyGroup = key.toString().split("[\\._]"); +// for (int i = 0; i < keyGroup.length; i++) { +// keyGroup[i] = keyGroup[i].toLowerCase(); +// keyGroup[i] = StringUtils.capitalize(keyGroup[i]); +// } +// String beanFieldNameWithCapitalization = StringUtils.join(keyGroup); +// try { +// setProperties(clazz, obj, "set" + beanFieldNameWithCapitalization, properties.getProperty(key.toString())); +// } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { +// //ignored... +// } +// } +// } catch (RuntimeException e) { +// log.warn("Error occurs !", e); +// } +// return obj; +// } } diff --git a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/OMSUtil.java b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/OMSUtil.java index 79a26dab65..d087acc3cb 100644 --- a/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/OMSUtil.java +++ b/eventmesh-connector-rocketmq/src/main/java/com/webank/eventmesh/connector/rocketmq/utils/OMSUtil.java @@ -16,26 +16,17 @@ */ package com.webank.eventmesh.connector.rocketmq.utils; -import com.webank.eventmesh.connector.rocketmq.common.Constants; -import com.webank.eventmesh.connector.rocketmq.domain.RocketMQConstants; -import com.webank.eventmesh.connector.rocketmq.domain.SendResultImpl; -import com.webank.eventmesh.connector.rocketmq.domain.BytesMessageImpl; -import io.openmessaging.BytesMessage; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.Message.BuiltinKeys; -import io.openmessaging.OMS; -import io.openmessaging.producer.SendResult; -import org.apache.rocketmq.client.producer.SendStatus; +import com.webank.eventmesh.common.Constants; +import io.openmessaging.api.Message; +import io.openmessaging.api.OMSBuiltinKeys; +import io.openmessaging.api.SendResult; +import io.openmessaging.api.exception.OMSRuntimeException; import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.message.MessageAccessor; import org.apache.rocketmq.common.message.MessageExt; import java.lang.reflect.Field; -import java.util.Iterator; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; +import java.util.*; public class OMSUtil { @@ -48,41 +39,83 @@ public static String buildInstanceName() { return Integer.toString(UtilAll.getPid()) + "%OpenMessaging" + "%" + System.nanoTime(); } - public static org.apache.rocketmq.common.message.Message msgConvert(BytesMessage omsMessage) { + public static org.apache.rocketmq.common.message.Message msgConvert(Message omsMessage) { org.apache.rocketmq.common.message.Message rmqMessage = new org.apache.rocketmq.common.message.Message(); - rmqMessage.setBody(omsMessage.getBody(byte[].class)); - - KeyValue sysHeaders = omsMessage.sysHeaders(); - KeyValue userHeaders = omsMessage.userHeaders(); + if (omsMessage == null) { + throw new OMSRuntimeException("'message' is null"); + } else { + if (omsMessage.getTopic() != null) { + rmqMessage.setTopic(omsMessage.getTopic()); + } + if (omsMessage.getKey() != null) { + rmqMessage.setKeys(omsMessage.getKey()); + } + if (omsMessage.getTag() != null) { + rmqMessage.setTags(omsMessage.getTag()); + } + if (omsMessage.getStartDeliverTime() > 0L) { + rmqMessage.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); + } - //All destinations in RocketMQ use Topic - rmqMessage.setTopic(sysHeaders.getString(BuiltinKeys.DESTINATION)); + if (omsMessage.getBody() != null) { + rmqMessage.setBody(omsMessage.getBody()); + } - if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { - long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); - if (deliverTime > 0) { - rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); + if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { + rmqMessage.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); } } + Properties systemProperties = omsMessage.getSystemProperties(); + Properties userProperties = omsMessage.getUserProperties(); + + //All destinations in RocketMQ use Topic +// rmqMessage.setTopic(systemProperties.getProperty(BuiltinKeys.DESTINATION)); + +// if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { +// long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); +// if (deliverTime > 0) { +// rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); +// } +// } - for (String key : userHeaders.keySet()) { - MessageAccessor.putProperty(rmqMessage, key, userHeaders.getString(key)); + for (String key : userProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessage, key, userProperties.getProperty(key)); } //System headers has a high priority - for (String key : sysHeaders.keySet()) { - MessageAccessor.putProperty(rmqMessage, key, sysHeaders.getString(key)); + for (String key : systemProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessage, key, systemProperties.getProperty(key)); } return rmqMessage; } - public static BytesMessage msgConvert(MessageExt rmqMsg) { - BytesMessage omsMsg = new BytesMessageImpl(); - omsMsg.setBody(rmqMsg.getBody()); + public static Message msgConvert(MessageExt rmqMsg) { + Message message = new Message(); + if (rmqMsg.getTopic() != null) { + message.setTopic(rmqMsg.getTopic()); + } + + if (rmqMsg.getKeys() != null) { + message.setKey(rmqMsg.getKeys()); + } + + if (rmqMsg.getTags() != null) { + message.setTag(rmqMsg.getTags()); + } - KeyValue headers = omsMsg.sysHeaders(); - KeyValue properties = omsMsg.userHeaders(); + if (rmqMsg.getBody() != null) { + message.setBody(rmqMsg.getBody()); + } + + if (rmqMsg.getUserProperty("TIMER_DELIVER_MS") != null) { + long ms = Long.parseLong(rmqMsg.getUserProperty("TIMER_DELIVER_MS")); + rmqMsg.getProperties().remove("TIMER_DELIVER_MS"); + message.setStartDeliverTime(ms); + } + + Properties systemProperties = new Properties(); + Properties userProperties = new Properties(); final Set> entries = rmqMsg.getProperties().entrySet(); @@ -90,66 +123,87 @@ public static BytesMessage msgConvert(MessageExt rmqMsg) { for (final Map.Entry entry : entries) { if (isOMSHeader(entry.getKey())) { //sysHeader - headers.put(entry.getKey(), entry.getValue()); + systemProperties.put(entry.getKey(), entry.getValue()); } else { //userHeader - properties.put(entry.getKey(), entry.getValue()); + userProperties.put(entry.getKey(), entry.getValue()); } } - omsMsg.putSysHeaders(BuiltinKeys.MESSAGE_ID, rmqMsg.getMsgId()); + systemProperties.put(Constants.PROPERTY_MESSAGE_MESSAGE_ID, rmqMsg.getMsgId()); - omsMsg.putSysHeaders(BuiltinKeys.DESTINATION, rmqMsg.getTopic()); + systemProperties.put(Constants.PROPERTY_MESSAGE_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()); + systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_HOST, String.valueOf(rmqMsg.getBornHost())); + systemProperties.put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, rmqMsg.getBornTimestamp()); + systemProperties.put(Constants.PROPERTY_MESSAGE_STORE_HOST, String.valueOf(rmqMsg.getStoreHost())); + systemProperties.put("STORE_TIMESTAMP", rmqMsg.getStoreTimestamp()); //use in manual ack - omsMsg.putUserHeaders(Constants.PROPERTY_MESSAGE_QUEUE_ID, rmqMsg.getQueueId()); - omsMsg.putUserHeaders(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET, rmqMsg.getQueueOffset()); - return omsMsg; + userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_ID, rmqMsg.getQueueId()); + userProperties.put(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET, rmqMsg.getQueueOffset()); + + message.setSystemProperties(systemProperties); + message.setUserProperties(userProperties); + + return message; } public static org.apache.rocketmq.common.message.MessageExt msgConvertExt(Message omsMessage) { + org.apache.rocketmq.common.message.MessageExt rmqMessageExt = new org.apache.rocketmq.common.message.MessageExt(); - rmqMessageExt.setBody(omsMessage.getBody(byte[].class)); + try { + if (omsMessage.getKey() != null) { + rmqMessageExt.setKeys(omsMessage.getKey()); + } + if (omsMessage.getTag() != null) { + rmqMessageExt.setTags(omsMessage.getTag()); + } + if (omsMessage.getStartDeliverTime() > 0L) { + rmqMessageExt.putUserProperty("TIMER_DELIVER_MS", String.valueOf(omsMessage.getStartDeliverTime())); + } - KeyValue sysHeaders = omsMessage.sysHeaders(); - KeyValue userHeaders = omsMessage.userHeaders(); + if (omsMessage.getBody() != null) { + rmqMessageExt.setBody(omsMessage.getBody()); + } - //All destinations in RocketMQ use Topic - rmqMessageExt.setTopic(sysHeaders.getString(BuiltinKeys.DESTINATION)); + if (omsMessage.getShardingKey() != null && !omsMessage.getShardingKey().isEmpty()) { + rmqMessageExt.putUserProperty("__SHARDINGKEY", omsMessage.getShardingKey()); + } - //use in manual ack - rmqMessageExt.setQueueId(userHeaders.getInt(Constants.PROPERTY_MESSAGE_QUEUE_ID)); - rmqMessageExt.setQueueOffset(userHeaders.getLong(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET)); + Properties systemProperties = omsMessage.getSystemProperties(); + Properties userProperties = omsMessage.getUserProperties(); + + //All destinations in RocketMQ use Topic + rmqMessageExt.setTopic(omsMessage.getTopic()); - if (sysHeaders.containsKey(BuiltinKeys.START_TIME)) { - long deliverTime = sysHeaders.getLong(BuiltinKeys.START_TIME, 0); - if (deliverTime > 0) { - rmqMessageExt.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); + int queueId = (int)userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_ID); + long queueOffset = (long)userProperties.get(Constants.PROPERTY_MESSAGE_QUEUE_OFFSET); + //use in manual ack + rmqMessageExt.setQueueId(queueId); + rmqMessageExt.setQueueOffset(queueOffset); + + for (String key : userProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessageExt, key, userProperties.getProperty(key)); } - } - for (String key : userHeaders.keySet()) { - MessageAccessor.putProperty(rmqMessageExt, key, userHeaders.getString(key)); - } + //System headers has a high priority + for (String key : systemProperties.stringPropertyNames()) { + MessageAccessor.putProperty(rmqMessageExt, key, systemProperties.getProperty(key)); + } - //System headers has a high priority - for (String key : sysHeaders.keySet()) { - MessageAccessor.putProperty(rmqMessageExt, key, sysHeaders.getString(key)); + } catch (Exception e) { + e.printStackTrace(); } - return rmqMessageExt; + } public static boolean isOMSHeader(String value) { - for (Field field : BuiltinKeys.class.getDeclaredFields()) { + for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { try { - if (field.get(BuiltinKeys.class).equals(value)) { + if (field.get(OMSBuiltinKeys.class).equals(value)) { return true; } } catch (IllegalAccessException e) { @@ -163,19 +217,21 @@ public static boolean isOMSHeader(String value) { * Convert a RocketMQ SEND_OK SendResult instance to a OMS SendResult. */ public static SendResult sendResultConvert(org.apache.rocketmq.client.producer.SendResult rmqResult) { - assert rmqResult.getSendStatus().equals(SendStatus.SEND_OK); - return new SendResultImpl(rmqResult.getMsgId(), OMS.newKeyValue()); + SendResult sendResult = new SendResult(); + sendResult.setTopic(rmqResult.getMessageQueue().getTopic()); + sendResult.setMessageId(rmqResult.getMsgId()); + return sendResult; } - public static KeyValue buildKeyValue(KeyValue... keyValues) { - KeyValue keyValue = OMS.newKeyValue(); - for (KeyValue properties : keyValues) { - for (String key : properties.keySet()) { - keyValue.put(key, properties.getString(key)); - } - } - return keyValue; - } +// public static KeyValue buildKeyValue(KeyValue... keyValues) { +// KeyValue keyValue = OMS.newKeyValue(); +// for (KeyValue properties : keyValues) { +// for (String key : properties.keySet()) { +// keyValue.put(key, properties.getString(key)); +// } +// } +// return keyValue; +// } /** * Returns an iterator that cycles indefinitely over the elements of {@code Iterable}. diff --git a/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java b/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java index 893a05048a..899f024701 100644 --- a/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java +++ b/eventmesh-connector-rocketmq/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java @@ -170,7 +170,7 @@ public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, Strin List msgs = new ArrayList(); msgs.add(msg); MessageQueue mq = new MessageQueue(); - mq.setBrokerName(brokerName); +// mq.setBrokerName(brokerName); mq.setTopic(msg.getTopic()); mq.setQueueId(msg.getQueueId()); diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/LocalMessageCacheTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/LocalMessageCacheTest.java deleted file mode 100644 index 33b19d1473..0000000000 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/LocalMessageCacheTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rocketmq.consumer; - -import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; -import com.webank.eventmesh.connector.rocketmq.consumer.LocalMessageCache; -import com.webank.eventmesh.connector.rocketmq.domain.ConsumeRequest; -import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; -import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; -import org.apache.rocketmq.common.message.MessageExt; -import org.apache.rocketmq.common.message.MessageQueue; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import static org.assertj.core.api.Assertions.assertThat; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class LocalMessageCacheTest { - private LocalMessageCache localMessageCache; - @Mock - private DefaultMQPullConsumer rocketmqPullConsume; - @Mock - private ConsumeRequest consumeRequest; - - @Before - public void init() { - ClientConfig clientConfig = new ClientConfig(); - clientConfig.setRmqPullMessageBatchNums(512); - clientConfig.setRmqPullMessageCacheCapacity(1024); - localMessageCache = new LocalMessageCache(rocketmqPullConsume, clientConfig); - } - - @Test - public void testNextPullBatchNums() throws Exception { - assertThat(localMessageCache.nextPullBatchNums()).isEqualTo(512); - for (int i = 0; i < 513; i++) { - localMessageCache.submitConsumeRequest(consumeRequest); - } - assertThat(localMessageCache.nextPullBatchNums()).isEqualTo(511); - } - - @Test - public void testNextPullOffset() throws Exception { - MessageQueue messageQueue = new MessageQueue(); - when(rocketmqPullConsume.fetchConsumeOffset(any(MessageQueue.class), anyBoolean())) - .thenReturn(123L); - assertThat(localMessageCache.nextPullOffset(new MessageQueue())).isEqualTo(123L); - } - - @Test - public void testUpdatePullOffset() throws Exception { - MessageQueue messageQueue = new MessageQueue(); - localMessageCache.updatePullOffset(messageQueue, 124L); - assertThat(localMessageCache.nextPullOffset(messageQueue)).isEqualTo(124L); - } - - @Test - public void testSubmitConsumeRequest() throws Exception { - byte[] body = new byte[] {'1', '2', '3'}; - MessageExt consumedMsg = new MessageExt(); - consumedMsg.setMsgId("NewMsgId"); - consumedMsg.setBody(body); - consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); - consumedMsg.setTopic("HELLO_QUEUE"); - - when(consumeRequest.getMessageExt()).thenReturn(consumedMsg); - localMessageCache.submitConsumeRequest(consumeRequest); - assertThat(localMessageCache.poll()).isEqualTo(consumedMsg); - } -} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PullConsumerImplTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PullConsumerImplTest.java deleted file mode 100644 index 930e520e30..0000000000 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PullConsumerImplTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rocketmq.consumer; - -import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; -import com.webank.eventmesh.connector.rocketmq.consumer.LocalMessageCache; -import com.webank.eventmesh.connector.rocketmq.consumer.PullConsumerImpl; -import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; -import io.openmessaging.*; -import io.openmessaging.consumer.PullConsumer; -import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; -import org.apache.rocketmq.common.message.MessageExt; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import java.lang.reflect.Field; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class PullConsumerImplTest { - private PullConsumer consumer; - private String queueName = "HELLO_QUEUE"; - - @Mock - private DefaultMQPullConsumer rocketmqPullConsumer; - private LocalMessageCache localMessageCache = null; - - @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(); - } - - @Test - public void testPoll() { - 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(queueName); - - when(localMessageCache.poll()).thenReturn(consumedMsg); - - Message message = consumer.receive(); - assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId"); - assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody); - } - - @Test - public void testPoll_WithTimeout() { - //There is a default timeout value, @see ClientConfig#omsOperationTimeout. - Message message = consumer.receive(); - assertThat(message).isNull(); - - message = consumer.receive(OMS.newKeyValue().put(Message.BuiltinKeys.TIMEOUT, 100)); - assertThat(message).isNull(); - } -} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PushConsumerImplTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PushConsumerImplTest.java index f4a04d74b2..630cdad03b 100644 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PushConsumerImplTest.java +++ b/eventmesh-connector-rocketmq/src/test/java/rocketmq/consumer/PushConsumerImplTest.java @@ -18,9 +18,7 @@ import com.webank.eventmesh.connector.rocketmq.consumer.PushConsumerImpl; import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; -import io.openmessaging.*; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.consumer.PushConsumer; +import io.openmessaging.api.*; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; import org.apache.rocketmq.common.message.MessageExt; @@ -32,13 +30,14 @@ import java.lang.reflect.Field; import java.util.Collections; +import java.util.Properties; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PushConsumerImplTest { - private PushConsumer consumer; + private Consumer consumer; @Mock private DefaultMQPushConsumer rocketmqPushConsumer; @@ -46,10 +45,10 @@ public class PushConsumerImplTest { @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")); + final MessagingAccessPoint messagingAccessPoint = OMS.builder().endpoint("oms:rocketmq://IP1:9876,IP2:9876/namespace").build(); + Properties consumerProp = new Properties(); + consumerProp.put("CONSUMER_ID", "TestGroup"); + consumer = messagingAccessPoint.createConsumer(consumerProp); Field field = PushConsumerImpl.class.getDeclaredField("rocketmqPushConsumer"); field.setAccessible(true); @@ -57,8 +56,7 @@ public void init() throws NoSuchFieldException, IllegalAccessException { field.set(consumer, rocketmqPushConsumer); //Replace when(rocketmqPushConsumer.getMessageListener()).thenReturn(innerConsumer.getMessageListener()); - messagingAccessPoint.startup(); - consumer.startup(); + consumer.start(); } @Test @@ -70,12 +68,12 @@ public void testConsumeMessage() { consumedMsg.setBody(testBody); consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC"); consumedMsg.setTopic("HELLO_QUEUE"); - consumer.attachQueue("HELLO_QUEUE", new MessageListener() { + consumer.subscribe("HELLO_QUEUE", "*", new AsyncMessageListener() { @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(); + public void consume(Message message, AsyncConsumeContext context) { + assertThat(message.getSystemProperties("MESSAGE_ID")).isEqualTo("NewMsgId"); + assertThat(message.getBody()).isEqualTo(testBody); + context.commit(Action.CommitMessage); } }); ((MessageListenerConcurrently) rocketmqPushConsumer diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/producer/ProducerImplTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/producer/ProducerImplTest.java index dd9003e150..b053213d14 100644 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/producer/ProducerImplTest.java +++ b/eventmesh-connector-rocketmq/src/test/java/rocketmq/producer/ProducerImplTest.java @@ -17,10 +17,10 @@ package rocketmq.producer; import com.webank.eventmesh.connector.rocketmq.producer.AbstractOMSProducer; -import io.openmessaging.MessagingAccessPoint; -import io.openmessaging.OMS; -import io.openmessaging.exception.OMSRuntimeException; -import io.openmessaging.producer.Producer; +import io.openmessaging.api.OMS; +import io.openmessaging.api.MessagingAccessPoint; +import io.openmessaging.api.Producer; +import io.openmessaging.api.exception.OMSRuntimeException; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.producer.DefaultMQProducer; @@ -35,6 +35,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.lang.reflect.Field; +import java.util.Properties; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown; @@ -51,16 +52,15 @@ public class ProducerImplTest { @Before public void init() throws NoSuchFieldException, IllegalAccessException { - final MessagingAccessPoint messagingAccessPoint = OMS - .getMessagingAccessPoint("oms:rocketmq://IP1:9876,IP2:9876/namespace"); - producer = messagingAccessPoint.createProducer(); + final MessagingAccessPoint messagingAccessPoint = OMS.builder().endpoint("oms:rocketmq://IP1:9876,IP2:9876/namespace").build(); + producer = messagingAccessPoint.createProducer(new Properties()); Field field = AbstractOMSProducer.class.getDeclaredField("rocketmqProducer"); field.setAccessible(true); field.set(producer, rocketmqProducer); - messagingAccessPoint.startup(); - producer.startup(); +// messagingAccessPoint.startup(); + producer.start(); } @Test @@ -69,10 +69,11 @@ public void testSend_OK() throws InterruptedException, RemotingException, MQClie sendResult.setMsgId("TestMsgID"); sendResult.setSendStatus(SendStatus.SEND_OK); when(rocketmqProducer.send(any(Message.class), anyLong())).thenReturn(sendResult); - io.openmessaging.producer.SendResult omsResult = - producer.send(producer.createBytesMessage("HELLO_TOPIC", new byte[] {'a'})); + io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[] {'a'}); + io.openmessaging.api.SendResult omsResult = + producer.send(message); - assertThat(omsResult.messageId()).isEqualTo("TestMsgID"); + assertThat(omsResult.getMessageId()).isEqualTo("TestMsgID"); } @Test @@ -82,7 +83,8 @@ public void testSend_Not_OK() throws InterruptedException, RemotingException, MQ when(rocketmqProducer.send(any(Message.class), anyLong())).thenReturn(sendResult); try { - producer.send(producer.createBytesMessage("HELLO_TOPIC", new byte[] {'a'})); + io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[] {'a'}); + producer.send(message); failBecauseExceptionWasNotThrown(OMSRuntimeException.class); } catch (Exception e) { assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed."); @@ -93,7 +95,8 @@ public void testSend_Not_OK() throws InterruptedException, RemotingException, MQ public void testSend_WithException() throws InterruptedException, RemotingException, MQClientException, MQBrokerException { when(rocketmqProducer.send(any(Message.class), anyLong())).thenThrow(MQClientException.class); try { - producer.send(producer.createBytesMessage("HELLO_TOPIC", new byte[] {'a'})); + io.openmessaging.api.Message message = new io.openmessaging.api.Message("HELLO_TOPIC", "", new byte[] {'a'}); + producer.send(message); failBecauseExceptionWasNotThrown(OMSRuntimeException.class); } catch (Exception e) { assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed."); diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/promise/DefaultPromiseTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/promise/DefaultPromiseTest.java index 42c72de777..825ee7986e 100644 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/promise/DefaultPromiseTest.java +++ b/eventmesh-connector-rocketmq/src/test/java/rocketmq/promise/DefaultPromiseTest.java @@ -1,121 +1,121 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rocketmq.promise; - -import com.webank.eventmesh.connector.rocketmq.promise.DefaultPromise; -import io.openmessaging.Future; -import io.openmessaging.FutureListener; -import io.openmessaging.Promise; -import io.openmessaging.exception.OMSRuntimeException; -import org.junit.Before; -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown; - -public class DefaultPromiseTest { - private Promise promise; - - @Before - public void init() { - promise = new DefaultPromise<>(); - } - - @Test - public void testIsCancelled() throws Exception { - assertThat(promise.isCancelled()).isEqualTo(false); - } - - @Test - public void testIsDone() throws Exception { - assertThat(promise.isDone()).isEqualTo(false); - promise.set("Done"); - assertThat(promise.isDone()).isEqualTo(true); - } - - @Test - public void testGet() throws Exception { - promise.set("Done"); - assertThat(promise.get()).isEqualTo("Done"); - } - - @Test - public void testGet_WithTimeout() throws Exception { - try { - promise.get(100); - failBecauseExceptionWasNotThrown(OMSRuntimeException.class); - } catch (OMSRuntimeException e) { - assertThat(e).hasMessageContaining("Get request result is timeout or interrupted"); - } - } - - @Test - public void testAddListener() throws Exception { - promise.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) { - assertThat(promise.get()).isEqualTo("Done"); - - } - }); - promise.set("Done"); - } - - @Test - public void testAddListener_ListenerAfterSet() throws Exception { - promise.set("Done"); - promise.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) { - assertThat(future.get()).isEqualTo("Done"); - } - }); - } - - @Test - public void testAddListener_WithException_ListenerAfterSet() throws Exception { - final Throwable exception = new OMSRuntimeException("-1", "Test Error"); - promise.setFailure(exception); - promise.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) { - assertThat(promise.getThrowable()).isEqualTo(exception); - } - }); - } - - @Test - public void testAddListener_WithException() throws Exception { - final Throwable exception = new OMSRuntimeException("-1", "Test Error"); - promise.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) { - assertThat(promise.getThrowable()).isEqualTo(exception); - } - }); - promise.setFailure(exception); - } - - @Test - public void getThrowable() throws Exception { - assertThat(promise.getThrowable()).isNull(); - Throwable exception = new OMSRuntimeException("-1", "Test Error"); - promise.setFailure(exception); - assertThat(promise.getThrowable()).isEqualTo(exception); - } - -} \ No newline at end of file +///* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package rocketmq.promise; +// +//import com.webank.eventmesh.connector.rocketmq.promise.DefaultPromise; +//import io.openmessaging.Future; +//import io.openmessaging.FutureListener; +//import io.openmessaging.Promise; +//import io.openmessaging.exception.OMSRuntimeException; +//import org.junit.Before; +//import org.junit.Test; +// +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.assertj.core.api.Fail.failBecauseExceptionWasNotThrown; +// +//public class DefaultPromiseTest { +// private Promise promise; +// +// @Before +// public void init() { +// promise = new DefaultPromise<>(); +// } +// +// @Test +// public void testIsCancelled() throws Exception { +// assertThat(promise.isCancelled()).isEqualTo(false); +// } +// +// @Test +// public void testIsDone() throws Exception { +// assertThat(promise.isDone()).isEqualTo(false); +// promise.set("Done"); +// assertThat(promise.isDone()).isEqualTo(true); +// } +// +// @Test +// public void testGet() throws Exception { +// promise.set("Done"); +// assertThat(promise.get()).isEqualTo("Done"); +// } +// +// @Test +// public void testGet_WithTimeout() throws Exception { +// try { +// promise.get(100); +// failBecauseExceptionWasNotThrown(OMSRuntimeException.class); +// } catch (OMSRuntimeException e) { +// assertThat(e).hasMessageContaining("Get request result is timeout or interrupted"); +// } +// } +// +// @Test +// public void testAddListener() throws Exception { +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.get()).isEqualTo("Done"); +// +// } +// }); +// promise.set("Done"); +// } +// +// @Test +// public void testAddListener_ListenerAfterSet() throws Exception { +// promise.set("Done"); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(future.get()).isEqualTo("Done"); +// } +// }); +// } +// +// @Test +// public void testAddListener_WithException_ListenerAfterSet() throws Exception { +// final Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.setFailure(exception); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// }); +// } +// +// @Test +// public void testAddListener_WithException() throws Exception { +// final Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.addListener(new FutureListener() { +// @Override +// public void operationComplete(Future future) { +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// }); +// promise.setFailure(exception); +// } +// +// @Test +// public void getThrowable() throws Exception { +// assertThat(promise.getThrowable()).isNull(); +// Throwable exception = new OMSRuntimeException("-1", "Test Error"); +// promise.setFailure(exception); +// assertThat(promise.getThrowable()).isEqualTo(exception); +// } +// +//} \ No newline at end of file diff --git a/eventmesh-connector-rocketmq/src/test/java/rocketmq/utils/BeanUtilsTest.java b/eventmesh-connector-rocketmq/src/test/java/rocketmq/utils/BeanUtilsTest.java index 7e2a8deb9b..b5a221ed91 100644 --- a/eventmesh-connector-rocketmq/src/test/java/rocketmq/utils/BeanUtilsTest.java +++ b/eventmesh-connector-rocketmq/src/test/java/rocketmq/utils/BeanUtilsTest.java @@ -19,14 +19,14 @@ import com.webank.eventmesh.connector.rocketmq.config.ClientConfig; import com.webank.eventmesh.connector.rocketmq.domain.NonStandardKeys; import com.webank.eventmesh.connector.rocketmq.utils.BeanUtils; -import io.openmessaging.KeyValue; -import io.openmessaging.OMS; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.Properties; + public class BeanUtilsTest { - private KeyValue properties = OMS.newKeyValue(); + private Properties properties = new Properties(); public static class CustomizedConfig extends ClientConfig { final static String STRING_TEST = "string.test"; diff --git a/eventmesh-runtime/conf/proxy.properties b/eventmesh-runtime/conf/proxy.properties index c6bde9ba88..39cd5609de 100644 --- a/eventmesh-runtime/conf/proxy.properties +++ b/eventmesh-runtime/conf/proxy.properties @@ -20,23 +20,11 @@ proxy.server.idc=DEFAULT proxy.server.env=PRD proxy.server.region=region1 proxy.server.cluster=COMMON -proxy.server.name=EVNETMESH-runtime +proxy.server.name=EVENTMESH-runtime proxy.server.dcn=010 proxy.sysid=0000 proxy.server.http.port=10105 -#######################defibus################## -proxy.server.defibus.namesrvAddr=127.0.0.1:9876;127.0.0.1:9876 -proxy.server.defibus.client.consumeThreadMin=2 -proxy.server.defibus.client.consumeThreadMax=2 -proxy.server.defibus.client.consumeThreadPoolQueueSize=10000 -proxy.server.defibus.client.pullBatchSize=32 -proxy.server.defibus.client.ackwindow=1000 -proxy.server.defibus.client.pubwindow=512 -proxy.server.defibus.client.pollNameServerInterval=10000 -proxy.server.defibus.client.heartbeatBrokerInterval=30000 -proxy.server.defibus.client.rebalanceInterval=20000 - ########################## proxy tcp configuration ############################ proxy.server.tcp.enabled=true proxy.server.tcp.port=10000 @@ -76,4 +64,4 @@ proxy.server.registry.registerIntervalInMills=10000 proxy.server.registry.fetchRegistryAddrIntervalInMills=20000 #auto-ack -proxy.server.defibus.client.comsumeTimeoutInMin=5 \ No newline at end of file +#proxy.server.defibus.client.comsumeTimeoutInMin=5 \ No newline at end of file diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQConsumerWrapper.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQConsumerWrapper.java index a0ef57bab1..c55f56ce0b 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQConsumerWrapper.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQConsumerWrapper.java @@ -19,12 +19,13 @@ import com.webank.eventmesh.api.AbstractContext; import com.webank.eventmesh.api.consumer.MeshMQPushConsumer; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.consumer.MessageListener; +import io.openmessaging.api.AsyncMessageListener; +import io.openmessaging.api.Message; +import io.openmessaging.api.MessageListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; +import java.util.Properties; import java.util.ServiceLoader; public class MQConsumerWrapper extends MQWrapper { @@ -33,7 +34,7 @@ public class MQConsumerWrapper extends MQWrapper { protected MeshMQPushConsumer meshMQPushConsumer; - public void subscribe(String topic, MessageListener listener) throws Exception { + public void subscribe(String topic, AsyncMessageListener listener) throws Exception { meshMQPushConsumer.subscribe(topic, listener); } @@ -41,15 +42,15 @@ public void unsubscribe(String topic) throws Exception { meshMQPushConsumer.unsubscribe(topic); } - public boolean isPause() { - return meshMQPushConsumer.isPause(); - } - - public void pause() { - meshMQPushConsumer.pause(); - } +// public boolean isPause() { +// return meshMQPushConsumer.isPause(); +// } +// +// public void pause() { +// meshMQPushConsumer.pause(); +// } - public synchronized void init(KeyValue keyValue) throws Exception { + public synchronized void init(Properties keyValue) throws Exception { meshMQPushConsumer = getMeshMQPushConsumer(); if (meshMQPushConsumer == null){ logger.error("can't load the meshMQPushConsumer plugin, please check."); diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQProducerWrapper.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQProducerWrapper.java index 700a9356c8..a016753556 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQProducerWrapper.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/plugin/MQProducerWrapper.java @@ -18,12 +18,13 @@ package com.webank.eventmesh.runtime.core.plugin; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.api.producer.MeshMQProducer; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +import java.util.Properties; import java.util.ServiceLoader; public class MQProducerWrapper extends MQWrapper { @@ -32,7 +33,7 @@ public class MQProducerWrapper extends MQWrapper { protected MeshMQProducer meshMQProducer; - public synchronized void init(KeyValue keyValue) throws Exception{ + public synchronized void init(Properties keyValue) throws Exception{ if (inited.get()) { return; } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java index a8a26f1b20..20a9921e84 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/HandleMsgContext.java @@ -23,7 +23,7 @@ import com.webank.eventmesh.runtime.core.consumergroup.ConsumerGroupConf; import com.webank.eventmesh.runtime.core.consumergroup.ConsumerGroupTopicConf; import com.webank.eventmesh.common.Constants; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,7 +79,7 @@ public HandleMsgContext(String msgRandomNo, String consumerGroup, ProxyConsumer this.bizSeqNo = bizSeqNo; this.uniqueId = uniqueId; this.consumeTopicConfig = consumeTopicConfig; - this.ttl = Integer.parseInt(msg.sysHeaders().getString(Message.BuiltinKeys.TIMEOUT)); + this.ttl = Integer.parseInt(msg.getUserProperties(Constants.PROPERTY_MESSAGE_TIMEOUT)); } public void addProp(String key, String val) { diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/ProxyConsumer.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/ProxyConsumer.java index 7482d5b605..66da38d88c 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/ProxyConsumer.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/consumer/ProxyConsumer.java @@ -18,7 +18,6 @@ package com.webank.eventmesh.runtime.core.protocol.http.consumer; import com.webank.eventmesh.api.AbstractContext; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; @@ -29,24 +28,16 @@ import com.webank.eventmesh.runtime.core.protocol.http.producer.SendMessageContext; import com.webank.eventmesh.runtime.core.protocol.http.push.HTTPMessageHandler; import com.webank.eventmesh.runtime.core.protocol.http.push.MessageHandler; -import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; -import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; import com.webank.eventmesh.runtime.domain.NonStandardKeys; import com.webank.eventmesh.runtime.patch.ProxyConsumeConcurrentlyStatus; import com.webank.eventmesh.runtime.util.ProxyUtil; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.OMS; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.producer.SendResult; -import org.apache.commons.collections4.CollectionUtils; +import io.openmessaging.api.*; import org.apache.commons.collections4.MapUtils; -import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Iterator; import java.util.List; +import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; public class ProxyConsumer { @@ -80,7 +71,7 @@ public ProxyConsumer(ProxyHTTPServer proxyHTTPServer, ConsumerGroupConf consumer public synchronized void init() throws Exception { httpMessageHandler = new HTTPMessageHandler(this); - KeyValue keyValue = OMS.newKeyValue(); + Properties keyValue = new Properties(); keyValue.put("isBroadcast", "false"); keyValue.put("consumerGroup", consumerGroupConf.getConsumerGroup()); keyValue.put("proxyIDC", proxyHTTPServer.getProxyConfiguration().proxyIDC); @@ -90,7 +81,7 @@ public synchronized void init() throws Exception { persistentMqConsumer.init(keyValue); // - KeyValue broadcastKeyValue = OMS.newKeyValue(); + Properties broadcastKeyValue = new Properties(); broadcastKeyValue.put("isBroadcast", "true"); broadcastKeyValue.put("consumerGroup", consumerGroupConf.getConsumerGroup()); broadcastKeyValue.put("proxyIDC", proxyHTTPServer.getProxyConfiguration().proxyIDC); @@ -112,16 +103,16 @@ public synchronized void start() throws Exception { } public void subscribe(String topic) throws Exception { - MessageListener listener = null; + AsyncMessageListener listener = null; if (!ProxyUtil.isBroadcast(topic)) { - listener = new MessageListener() { + listener = new AsyncMessageListener() { @Override - public void onReceived(Message message, Context context) { - String topic = message.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); - String bizSeqNo = message.sysHeaders().getString(Message.BuiltinKeys.SEARCH_KEYS); - String uniqueId = message.userHeaders().getString(Constants.RMB_UNIQ_ID); + public void consume(Message message, AsyncConsumeContext context) { + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + String bizSeqNo = message.getSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS); + String uniqueId = message.getUserProperties(Constants.RMB_UNIQ_ID); - message.userHeaders().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getUserProperties().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); if (messageLogger.isDebugEnabled()) { messageLogger.debug("message|mq2proxy|topic={}|msg={}", topic, message); } else { @@ -134,8 +125,9 @@ public void onReceived(Message message, Context context) { logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); try { sendMessageBack(message, uniqueId, bizSeqNo); - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); return; } catch (Exception ex) { } @@ -144,29 +136,31 @@ public void onReceived(Message message, Context context) { topic, message, persistentMqConsumer.getContext(), consumerGroupConf, proxyHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); if (httpMessageHandler.handle(handleMsgContext)) { - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + context.commit(Action.CommitMessage); } else { try { sendMessageBack(message, uniqueId, bizSeqNo); } catch (Exception e) { } - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); } } }; persistentMqConsumer.subscribe(topic, listener); } else { - listener = new MessageListener() { + listener = new AsyncMessageListener() { @Override - public void onReceived(Message message, Context context) { - String topic = message.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); - String bizSeqNo = message.sysHeaders().getString(Message.BuiltinKeys.SEARCH_KEYS); - String uniqueId = message.userHeaders().getString(Constants.RMB_UNIQ_ID); + public void consume(Message message, AsyncConsumeContext context) { + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + String bizSeqNo = message.getSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS); + String uniqueId = message.getUserProperties(Constants.RMB_UNIQ_ID); - message.userHeaders().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getUserProperties().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); if (messageLogger.isDebugEnabled()) { messageLogger.debug("message|mq2proxy|topic={}|msg={}", topic, message); @@ -180,8 +174,9 @@ public void onReceived(Message message, Context context) { logger.error("no topicConfig found, consumerGroup:{} topic:{}", consumerGroupConf.getConsumerGroup(), topic); try { sendMessageBack(message, uniqueId, bizSeqNo); - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); return; } catch (Exception ex) { } @@ -190,16 +185,18 @@ public void onReceived(Message message, Context context) { topic, message, broadcastMqConsumer.getContext(), consumerGroupConf, proxyHTTPServer, bizSeqNo, uniqueId, currentTopicConfig); if (httpMessageHandler.handle(handleMsgContext)) { - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + context.commit(Action.CommitMessage); } else { try { sendMessageBack(message, uniqueId, bizSeqNo); } catch (Exception e) { } - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); } } }; @@ -215,14 +212,14 @@ public void unsubscribe(String topic) throws Exception { } } - public boolean isPause() { - return persistentMqConsumer.isPause() && broadcastMqConsumer.isPause(); - } - - public void pause() { - persistentMqConsumer.pause(); - broadcastMqConsumer.pause(); - } +// public boolean isPause() { +// return persistentMqConsumer.isPause() && broadcastMqConsumer.isPause(); +// } +// +// public void pause() { +// persistentMqConsumer.pause(); +// broadcastMqConsumer.pause(); +// } public synchronized void shutdown() throws Exception { persistentMqConsumer.shutdown(); @@ -266,9 +263,14 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqno:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); } + +// @Override +// public void onException(Throwable e) { +// logger.warn("consumer:{} consume fail, sendMessageBack, bizSeqno:{}, uniqueId:{}", consumerGroupConf.getConsumerGroup(), bizSeqNo, uniqueId); +// } }); } } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java index 0a377d4883..3be09ec008 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageProcessor.java @@ -17,7 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.http.processor; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.http.async.AsyncContext; @@ -32,13 +32,13 @@ import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchRequestHeader; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchResponseHeader; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.util.RemotingHelper; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -144,20 +144,22 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext try { // Message rocketMQMsg; - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); + // topic + omsMsg.setTopic(msg.topic); // body omsMsg.setBody(msg.msg.getBytes(ProxyConstants.DEFAULT_CHARSET)); if (!StringUtils.isBlank(msg.tag)) { - omsMsg.putUserHeaders(ProxyConstants.TAG, msg.tag); + omsMsg.putUserProperties(ProxyConstants.TAG, msg.tag); } // if (StringUtils.isBlank(msg.tag)) { // rocketMQMsg = new Message(msg.topic, msg.msg.getBytes(ProxyConstants.DEFAULT_CHARSET)); // } else { // rocketMQMsg = new Message(msg.topic, msg.tag, msg.msg.getBytes(ProxyConstants.DEFAULT_CHARSET)); // } - omsMsg.putUserHeaders("msgType", "persistent"); + omsMsg.putUserProperties("msgType", "persistent"); // ttl - omsMsg.putSysHeaders(Message.BuiltinKeys.TIMEOUT, msg.ttl); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, msg.ttl); //MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, msg.ttl); msgList.add(omsMsg); if (topicBatchMessageMappings.containsKey(msg.topic)) { @@ -185,12 +187,12 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext return; } - proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(Integer.valueOf(sendMessageBatchRequestBody.getSize())); + proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsg(Integer.parseInt(sendMessageBatchRequestBody.getSize())); if (proxyHTTPServer.getProxyConfiguration().proxyServerBatchMsgBatchEnabled) { for (List batchMsgs : topicBatchMessageMappings.values()) { // TODO:api中的实现,考虑是否放到插件中 - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); // try { // msgBatch = msgBatch.generateFromList(batchMsgs); // for (Message message : msgBatch.getMessages()) { @@ -211,10 +213,16 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { - batchMessageLogger.warn("", e); + public void onException(OnExceptionContext context) { + batchMessageLogger.warn("", context.getException()); proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); } + +// @Override +// public void onException(Throwable e) { +// batchMessageLogger.warn("", e); +// proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// } }); } } else { @@ -227,10 +235,16 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { - batchMessageLogger.warn("", e); + public void onException(OnExceptionContext context) { + batchMessageLogger.warn("", context.getException()); proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); } + +// @Override +// public void onException(Throwable e) { +// batchMessageLogger.warn("", e); +// proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// } }); } } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java index eb1cd6c5f5..ffcad35a52 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/BatchSendMessageV2Processor.java @@ -17,7 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.http.processor; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.http.async.AsyncContext; @@ -32,13 +32,13 @@ import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageBatchV2ResponseHeader; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.util.RemotingHelper; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,7 +134,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext } // Message rocketMQMsg = null; - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); try { // if (StringUtils.isBlank(sendMessageBatchV2RequestBody.getTag())) { @@ -146,13 +146,15 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext // body omsMsg.setBody(sendMessageBatchV2RequestBody.getMsg().getBytes(ProxyConstants.DEFAULT_CHARSET)); // topic - omsMsg.putSysHeaders(Message.BuiltinKeys.DESTINATION, sendMessageBatchV2RequestBody.getTopic()); + // topic + omsMsg.setTopic(sendMessageBatchV2RequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageBatchV2RequestBody.getTopic()); if (!StringUtils.isBlank(sendMessageBatchV2RequestBody.getTag())) { - omsMsg.putUserHeaders(ProxyConstants.TAG, sendMessageBatchV2RequestBody.getTag()); + omsMsg.putUserProperties(ProxyConstants.TAG, sendMessageBatchV2RequestBody.getTag()); } - omsMsg.putUserHeaders("msgType", "persistent"); + omsMsg.putUserProperties("msgType", "persistent"); // ttl - omsMsg.putSysHeaders(Message.BuiltinKeys.TIMEOUT, sendMessageBatchV2RequestBody.getTtl()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, sendMessageBatchV2RequestBody.getTtl()); // rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); // MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, sendMessageBatchV2RequestBody.getTtl()); @@ -187,15 +189,26 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { long batchEndTime = System.currentTimeMillis(); proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); batchMessageLogger.error("batchMessageV2|proxy2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", sendMessageBatchV2RequestBody.getBizSeqNo(), batchEndTime - batchStartTime, - sendMessageBatchV2RequestBody.getTopic(), e); + sendMessageBatchV2RequestBody.getTopic(), context.getException()); } + +// @Override +// public void onException(Throwable e) { +// long batchEndTime = System.currentTimeMillis(); +// proxyHTTPServer.getHttpRetryer().pushRetry(sendMessageContext.delay(10000)); +// proxyHTTPServer.metrics.summaryMetrics.recordBatchSendMsgCost(batchEndTime - batchStartTime); +// batchMessageLogger.error("batchMessageV2|proxy2mq|REQ|ASYNC|bizSeqNo={}|send2MQCost={}ms|topic={}", +// sendMessageBatchV2RequestBody.getBizSeqNo(), +// batchEndTime - batchStartTime, +// sendMessageBatchV2RequestBody.getTopic(), e); +// } }); } catch (Exception e) { responseProxyCommand = asyncContext.getRequest().createHttpCommandResponse( diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java index d5e2b43ae5..8df1ab07ef 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/ReplyMessageProcessor.java @@ -17,7 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.http.processor; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.http.async.AsyncContext; @@ -35,13 +35,13 @@ import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.webank.eventmesh.common.protocol.http.header.message.ReplyMessageRequestHeader; import com.webank.eventmesh.common.protocol.http.header.message.ReplyMessageResponseHeader; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.util.RemotingHelper; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -119,7 +119,7 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext long startTime = System.currentTimeMillis(); // Message rocketMQMsg; - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); String replyTopic = ProxyConstants.RR_REPLY_TOPIC; Map extFields = replyMessageRequestBody.getExtFields(); @@ -137,17 +137,18 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext try { // body omsMsg.setBody(replyMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); + omsMsg.setTopic(replyTopic); // topic - omsMsg.putSysHeaders(Message.BuiltinKeys.DESTINATION, replyTopic); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, replyTopic); // if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { // omsMsg.putUserHeaders("Tag", sendMessageRequestBody.getTag()); // } // rocketMQMsg = new Message(replyTopic, // replyMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); - omsMsg.putUserHeaders("msgType", "persistent"); + omsMsg.putUserProperties("msgType", "persistent"); // rocketMQMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.PERSISTENT); for (Map.Entry entry : extFields.entrySet()) { - omsMsg.putUserHeaders(entry.getKey(), entry.getValue()); + omsMsg.putUserProperties(entry.getKey(), entry.getValue()); } // //for rocketmq support @@ -155,9 +156,9 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext // MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_CORRELATION_ID, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID)); // MessageAccessor.putProperty(rocketMQMsg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, rocketMQMsg.getProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO)); // ttl - omsMsg.putSysHeaders(Message.BuiltinKeys.TIMEOUT, String.valueOf(ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, String.valueOf(ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); // MessageAccessor.putProperty(rocketMQMsg, DeFiBusConstant.PROPERTY_MESSAGE_TTL, String.valueOf(ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)); - omsMsg.putUserHeaders(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); if (messageLogger.isDebugEnabled()) { messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", replyMessageRequestBody.getBizSeqNo(), replyTopic); @@ -195,7 +196,7 @@ public void onResponse(HttpCommand httpCommand) { replyMessageRequestBody.getContent()); try { - sendMessageContext.getMsg().userHeaders().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + sendMessageContext.getMsg().getUserProperties().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); proxyProducer.reply(sendMessageContext, new SendCallback() { @Override public void onSuccess(SendResult sendResult) { @@ -214,11 +215,11 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( replyMessageResponseHeader, SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_REPLY_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); + ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(context.getException(), 2))); asyncContext.onComplete(err, handler); long endTime = System.currentTimeMillis(); proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); @@ -228,8 +229,26 @@ public void onException(Throwable e) { replyMQCluster + "-" + ProxyConstants.RR_REPLY_TOPIC, replyMessageRequestBody.getOrigTopic(), replyMessageRequestBody.getBizSeqNo(), - replyMessageRequestBody.getUniqueId(), e); + replyMessageRequestBody.getUniqueId(), context.getException()); } + +// @Override +// public void onException(Throwable e) { +// HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( +// replyMessageResponseHeader, +// SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_REPLY_MSG_ERR.getRetCode(), +// ProxyRetCode.PROXY_REPLY_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); +// asyncContext.onComplete(err, handler); +// long endTime = System.currentTimeMillis(); +// proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgFailed(); +// proxyHTTPServer.metrics.summaryMetrics.recordReplyMsgCost(endTime - startTime); +// messageLogger.error("message|proxy2mq|RSP|SYNC|reply2MQCost={}|topic={}|origTopic={}|bizSeqNo={}|uniqueId={}", +// endTime - startTime, +// replyMQCluster + "-" + ProxyConstants.RR_REPLY_TOPIC, +// replyMessageRequestBody.getOrigTopic(), +// replyMessageRequestBody.getBizSeqNo(), +// replyMessageRequestBody.getUniqueId(), e); +// } }); } catch (Exception ex) { HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java index 56884d2521..80cf354589 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendAsyncMessageProcessor.java @@ -17,7 +17,6 @@ package com.webank.eventmesh.runtime.core.protocol.http.processor; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.http.async.AsyncContext; @@ -35,13 +34,13 @@ import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.util.RemotingHelper; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -122,24 +121,25 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext ttl = sendMessageRequestBody.getTtl(); } - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); try { // body omsMsg.setBody(sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); // topic - omsMsg.putSysHeaders(Message.BuiltinKeys.DESTINATION, sendMessageRequestBody.getTopic()); + omsMsg.setTopic(sendMessageRequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageRequestBody.getTopic()); if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { - omsMsg.putUserHeaders(ProxyConstants.TAG, sendMessageRequestBody.getTag()); + omsMsg.putUserProperties(ProxyConstants.TAG, sendMessageRequestBody.getTag()); } // ttl - omsMsg.putSysHeaders(Message.BuiltinKeys.TIMEOUT, ttl); + omsMsg.putUserProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, ttl); // bizNo - omsMsg.putSysHeaders(Message.BuiltinKeys.SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); - omsMsg.putUserHeaders("msgType", "persistent"); - omsMsg.putUserHeaders(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - omsMsg.putUserHeaders(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); - omsMsg.putUserHeaders(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); + omsMsg.putUserProperties("msgType", "persistent"); + omsMsg.putUserProperties(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); + omsMsg.putUserProperties(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); // new rocketmq client can't support put DeFiBusConstant.PROPERTY_MESSAGE_TTL // rocketMQMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, ttl); @@ -183,7 +183,7 @@ public void onResponse(HttpCommand httpCommand) { .setProp(sendMessageRequestBody.getExtFields()); try { - sendMessageContext.getMsg().userHeaders().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + sendMessageContext.getMsg().getUserProperties().put(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); proxyProducer.send(sendMessageContext, new SendCallback() { @Override @@ -202,11 +202,11 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); + ProxyRetCode.PROXY_SEND_ASYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(context.getException(), 2))); asyncContext.onComplete(err, handler); long endTime = System.currentTimeMillis(); proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); @@ -215,7 +215,7 @@ public void onException(Throwable e) { endTime - startTime, sendMessageRequestBody.getTopic(), sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), e); + sendMessageRequestBody.getUniqueId(), context.getException()); } }); } catch (Exception ex) { diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java index 00a5604124..c02c9044cb 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/processor/SendSyncMessageProcessor.java @@ -18,7 +18,6 @@ package com.webank.eventmesh.runtime.core.protocol.http.processor; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.http.async.AsyncContext; @@ -36,15 +35,15 @@ import com.webank.eventmesh.common.protocol.http.common.RequestCode; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader; import com.webank.eventmesh.common.protocol.http.header.message.SendMessageResponseHeader; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; import com.webank.eventmesh.runtime.util.OMSUtil; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.alibaba.fastjson.JSON; import com.webank.eventmesh.runtime.util.RemotingHelper; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -122,29 +121,30 @@ public void processRequest(ChannelHandlerContext ctx, AsyncContext ttl = sendMessageRequestBody.getTtl(); } - BytesMessage omsMsg = new BytesMessageImpl(); + Message omsMsg = new Message(); try { // body omsMsg.setBody(sendMessageRequestBody.getContent().getBytes(ProxyConstants.DEFAULT_CHARSET)); // topic - omsMsg.putSysHeaders(Message.BuiltinKeys.DESTINATION, sendMessageRequestBody.getTopic()); + omsMsg.setTopic(sendMessageRequestBody.getTopic()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION, sendMessageRequestBody.getTopic()); if (!StringUtils.isBlank(sendMessageRequestBody.getTag())) { - omsMsg.putUserHeaders("Tag", sendMessageRequestBody.getTag()); + omsMsg.putUserProperties("Tag", sendMessageRequestBody.getTag()); } // ttl - omsMsg.putSysHeaders(Message.BuiltinKeys.TIMEOUT, ttl); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_TIMEOUT, ttl); // bizNo - omsMsg.putSysHeaders(Message.BuiltinKeys.SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); - omsMsg.putUserHeaders("msgType", "persistent"); - omsMsg.putUserHeaders(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - omsMsg.putUserHeaders(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); - omsMsg.putUserHeaders("REPLY_TO", proxyProducer.getMqProducerWrapper().getMeshMQProducer().buildMQClientId()); + omsMsg.putSystemProperties(Constants.PROPERTY_MESSAGE_SEARCH_KEYS, sendMessageRequestBody.getBizSeqNo()); + omsMsg.putUserProperties("msgType", "persistent"); + omsMsg.putUserProperties(ProxyConstants.REQ_C2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(Constants.RMB_UNIQ_ID, sendMessageRequestBody.getUniqueId()); + omsMsg.putUserProperties("REPLY_TO", proxyProducer.getMqProducerWrapper().getMeshMQProducer().buildMQClientId()); if (messageLogger.isDebugEnabled()) { messageLogger.debug("msg2MQMsg suc, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), sendMessageRequestBody.getTopic()); } - omsMsg.putUserHeaders(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.putUserProperties(ProxyConstants.REQ_PROXY2MQ_TIMESTAMP, String.valueOf(System.currentTimeMillis())); } catch (Exception e) { messageLogger.error("msg2MQMsg err, bizSeqNo={}, topic={}", sendMessageRequestBody.getBizSeqNo(), sendMessageRequestBody.getTopic(), e); @@ -193,11 +193,11 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getRetCode(), - ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); + ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(context.getException(), 2))); asyncContext.onComplete(err, handler); long endTime = System.currentTimeMillis(); proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); @@ -206,16 +206,32 @@ public void onException(Throwable e) { endTime - startTime, sendMessageRequestBody.getTopic(), sendMessageRequestBody.getBizSeqNo(), - sendMessageRequestBody.getUniqueId(), e); + sendMessageRequestBody.getUniqueId(), context.getException()); } +// } +// +// @Override +// public void onException(Throwable e) { +// HttpCommand err = asyncContext.getRequest().createHttpCommandResponse( +// sendMessageResponseHeader, +// SendMessageResponseBody.buildBody(ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getRetCode(), +// ProxyRetCode.PROXY_SEND_SYNC_MSG_ERR.getErrMsg() + ProxyUtil.stackTrace(e, 2))); +// asyncContext.onComplete(err, handler); +// long endTime = System.currentTimeMillis(); +// proxyHTTPServer.metrics.summaryMetrics.recordSendMsgFailed(); +// proxyHTTPServer.metrics.summaryMetrics.recordSendMsgCost(endTime - startTime); +// messageLogger.error("message|proxy2mq|REQ|SYNC|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", +// endTime - startTime, +// sendMessageRequestBody.getTopic(), +// sendMessageRequestBody.getBizSeqNo(), +// sendMessageRequestBody.getUniqueId(), e); +// } }, new RRCallback() { @Override public void onSuccess(Message omsMsg) { - omsMsg.userHeaders().put(ProxyConstants.BORN_TIMESTAMP, omsMsg.sysHeaders(). - getString(Message.BuiltinKeys.BORN_TIMESTAMP)); - omsMsg.userHeaders().put(ProxyConstants.STORE_TIMESTAMP, omsMsg.sysHeaders(). - getString(Message.BuiltinKeys.STORE_TIMESTAMP)); - omsMsg.userHeaders().put(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + omsMsg.getUserProperties().put(Constants.PROPERTY_MESSAGE_BORN_TIMESTAMP, omsMsg.getSystemProperties("BORN_TIMESTAMP")); + omsMsg.getUserProperties().put(ProxyConstants.STORE_TIMESTAMP, omsMsg.getSystemProperties("STORE_TIMESTAMP")); + omsMsg.getUserProperties().put(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); messageLogger.info("message|mq2proxy|RSP|SYNC|rrCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", System.currentTimeMillis() - startTime, sendMessageRequestBody.getTopic(), @@ -223,15 +239,14 @@ public void onSuccess(Message omsMsg) { sendMessageRequestBody.getUniqueId()); try { - final String rtnMsg = new String(omsMsg.getBody(byte[].class), ProxyConstants.DEFAULT_CHARSET); - omsMsg.userHeaders().put(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + final String rtnMsg = new String(omsMsg.getBody(), ProxyConstants.DEFAULT_CHARSET); + omsMsg.getUserProperties().put(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); HttpCommand succ = asyncContext.getRequest().createHttpCommandResponse( sendMessageResponseHeader, SendMessageResponseBody.buildBody(ProxyRetCode.SUCCESS.getRetCode(), - JSON.toJSONString(new SendMessageResponseBody.ReplyMessage(omsMsg.sysHeaders(). - getString(Message.BuiltinKeys.DESTINATION), rtnMsg, - OMSUtil.combineProp(OMSUtil.convertKeyValue2Prop(omsMsg.sysHeaders()), - OMSUtil.convertKeyValue2Prop(omsMsg.userHeaders()))) + JSON.toJSONString(new SendMessageResponseBody.ReplyMessage(omsMsg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), rtnMsg, + OMSUtil.combineProp(omsMsg.getSystemProperties(), + omsMsg.getUserProperties())) ))); asyncContext.onComplete(succ, handler); } catch (Exception ex) { diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/ProxyProducer.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/ProxyProducer.java index 1199eed5ec..85fc6ac905 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/ProxyProducer.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/ProxyProducer.java @@ -18,17 +18,16 @@ package com.webank.eventmesh.runtime.core.protocol.http.producer; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.runtime.configuration.ProxyConfiguration; import com.webank.eventmesh.runtime.core.consumergroup.ProducerGroupConf; import com.webank.eventmesh.runtime.core.plugin.MQProducerWrapper; import com.webank.eventmesh.runtime.util.ProxyUtil; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.OMS; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; public class ProxyProducer { @@ -79,7 +78,7 @@ public synchronized void init(ProxyConfiguration proxyConfiguration, ProducerGro this.producerGroupConfig = producerGroupConfig; this.proxyConfiguration = proxyConfiguration; - KeyValue keyValue = OMS.newKeyValue(); + Properties keyValue = new Properties(); keyValue.put("producerGroup", producerGroupConfig.getGroupName()); keyValue.put("instanceName", ProxyUtil.buildProxyClientID(producerGroupConfig.getGroupName(), proxyConfiguration.proxyRegion, proxyConfiguration.proxyCluster)); diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java index 639fa76ef3..432cdebedd 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/producer/SendMessageContext.java @@ -17,12 +17,13 @@ package com.webank.eventmesh.runtime.core.protocol.http.producer; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.runtime.boot.ProxyHTTPServer; import com.webank.eventmesh.runtime.core.protocol.http.retry.RetryContext; import com.webank.eventmesh.common.Constants; -import io.openmessaging.Message; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.Message; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,14 +135,14 @@ public boolean retry() throws Exception { @Override public void onSuccess(SendResult sendResult) { - } @Override - public void onException(Throwable e) { - logger.warn("", e); + public void onException(OnExceptionContext context) { + logger.warn("", context.getException()); proxyHTTPServer.metrics.summaryMetrics.recordSendBatchMsgFailed(1); } + }); return true; diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java index b31b42c2a3..47104ac3c3 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/http/push/AsyncHTTPPushRequest.java @@ -100,11 +100,11 @@ public void tryHTTPRequest() { builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYREGION, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyRegion); builder.addHeader(ProtocolKey.ProxyInstanceKey.PROXYIDC, handleMsgContext.getProxyHTTPServer().getProxyConfiguration().proxyIDC); - handleMsgContext.getMsg().userHeaders().put(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + handleMsgContext.getMsg().getUserProperties().put(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); String content = ""; try { - content = new String(handleMsgContext.getMsg().getBody(byte[].class), ProxyConstants.DEFAULT_CHARSET); + content = new String(handleMsgContext.getMsg().getBody(), ProxyConstants.DEFAULT_CHARSET); } catch (Exception ex) { return; } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java index 2e183dc51e..105f3e1e19 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientGroupWrapper.java @@ -19,7 +19,7 @@ import com.alibaba.fastjson.JSON; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.core.plugin.MQConsumerWrapper; import com.webank.eventmesh.runtime.core.plugin.MQProducerWrapper; import com.webank.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.DownstreamDispatchStrategy; @@ -36,11 +36,7 @@ import com.webank.eventmesh.runtime.domain.NonStandardKeys; import com.webank.eventmesh.runtime.metrics.tcp.ProxyTcpMonitor; import com.webank.eventmesh.runtime.patch.ProxyConsumeConcurrentlyStatus; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; -import io.openmessaging.OMS; -import io.openmessaging.consumer.MessageListener; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.*; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -148,9 +144,11 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { - String bizSeqNo = upStreamMsgContext.getMsg().sysHeaders().getString(ProxyConstants.PROPERTY_MESSAGE_KEYS); - logger.error("reply err! topic:{}, bizSeqNo:{}, client:{}", upStreamMsgContext.getMsg().sysHeaders().getString(Message.BuiltinKeys.DESTINATION), bizSeqNo, upStreamMsgContext.getSession().getClient(), e); + public void onException(OnExceptionContext context) { + String bizSeqNo = upStreamMsgContext.getMsg().getSystemProperties(ProxyConstants.PROPERTY_MESSAGE_KEYS); + logger.error("reply err! topic:{}, bizSeqNo:{}, client:{}", + upStreamMsgContext.getMsg().getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), bizSeqNo, + upStreamMsgContext.getSession().getClient(), context.getException()); } }); return true; @@ -226,7 +224,8 @@ public synchronized void startClientGroupProducer() throws Exception { return; } - KeyValue keyValue = OMS.newKeyValue(); + Properties keyValue = new Properties(); +// KeyValue keyValue = OMS.newKeyValue(); keyValue.put("producerGroup", groupName); keyValue.put("instanceName", ProxyUtil.buildProxyTcpClientID(sysId, dcn, "PUB", accessConfiguration.proxyCluster)); @@ -348,7 +347,7 @@ public synchronized void initClientGroupPersistentConsumer() throws Exception { return; } - KeyValue keyValue = OMS.newKeyValue(); + Properties keyValue = new Properties(); keyValue.put("isBroadcast", "false"); keyValue.put("consumerGroup", groupName); keyValue.put("proxyIDC", accessConfiguration.proxyIDC); @@ -437,7 +436,7 @@ public synchronized void initClientGroupBroadcastConsumer() throws Exception { return; } - KeyValue keyValue = OMS.newKeyValue(); + Properties keyValue = new Properties(); keyValue.put("isBroadcast", "true"); keyValue.put("consumerGroup", groupName); keyValue.put("proxyIDC", accessConfiguration.proxyIDC); @@ -508,21 +507,22 @@ public synchronized void startClientGroupBroadcastConsumer() throws Exception{ } public void subscribe(String topic) throws Exception { - MessageListener listener = null; + AsyncMessageListener listener = null; if (ProxyUtil.isBroadcast(topic)) { - listener = new MessageListener() { + listener = new AsyncMessageListener() { @Override - public void onReceived(Message message, Context context) { + public void consume(Message message, AsyncConsumeContext context) { proxyTcpMonitor.getMq2proxyMsgNum().incrementAndGet(); - String topic = message.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); - message.sysHeaders().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - message.sysHeaders().put(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + message.getSystemProperties().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getSystemProperties().put(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); if(CollectionUtils.isEmpty(groupConsumerSessions)){ logger.warn("found no session to downstream broadcast msg"); - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); return; } @@ -545,24 +545,25 @@ public void onReceived(Message message, Context context) { } logger.warn("downstream broadcast msg,session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION)) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; + long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION)) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; downStreamMsgContext.delay(delayTime); proxyTcpRetryer.pushRetry(downStreamMsgContext); } - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + context.commit(Action.CommitMessage); } }; broadCastMsgConsumer.subscribe(topic, listener); } else { - listener = new MessageListener() { + listener = new AsyncMessageListener() { @Override - public void onReceived(Message message, Context context) { + public void consume(Message message, AsyncConsumeContext context) { proxyTcpMonitor.getMq2proxyMsgNum().incrementAndGet(); - String topic = message.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); - message.sysHeaders().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - message.sysHeaders().put(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); + String topic = message.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); + message.getSystemProperties().put(ProxyConstants.REQ_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + message.getSystemProperties().put(ProxyConstants.REQ_RECEIVE_PROXY_IP, accessConfiguration.proxyServerIp); Session session = downstreamDispatchStrategy.select(groupName, topic, groupConsumerSessions); String bizSeqNo = ProxyUtil.getMessageBizSeq(message); @@ -570,11 +571,11 @@ public void onReceived(Message message, Context context) { try { Integer sendBackTimes = new Integer(0); String sendBackFromProxyIp = ""; - if(StringUtils.isNotBlank(message.sysHeaders().getString(ProxyConstants.PROXY_SEND_BACK_TIMES))){ - sendBackTimes = Integer.valueOf(message.sysHeaders().getString(ProxyConstants.PROXY_SEND_BACK_TIMES)); + if(StringUtils.isNotBlank(message.getSystemProperties(ProxyConstants.PROXY_SEND_BACK_TIMES))){ + sendBackTimes = Integer.valueOf(message.getSystemProperties(ProxyConstants.PROXY_SEND_BACK_TIMES)); } - if(StringUtils.isNotBlank(message.sysHeaders().getString(ProxyConstants.PROXY_SEND_BACK_IP))){ - sendBackFromProxyIp = message.sysHeaders().getString(ProxyConstants.PROXY_SEND_BACK_IP); + if(StringUtils.isNotBlank(message.getSystemProperties(ProxyConstants.PROXY_SEND_BACK_IP))){ + sendBackFromProxyIp = message.getSystemProperties(ProxyConstants.PROXY_SEND_BACK_IP); } logger.error("found no session to downstream msg,groupName:{}, topic:{}, bizSeqNo:{}", groupName, topic, bizSeqNo); @@ -583,16 +584,17 @@ public void onReceived(Message message, Context context) { logger.error("sendBack to broker over max times:{}, groupName:{}, topic:{}, bizSeqNo:{}", proxyTCPServer.getAccessConfiguration().proxyTcpSendBackMaxTimes, groupName, topic, bizSeqNo); } else { sendBackTimes++; - message.sysHeaders().put(ProxyConstants.PROXY_SEND_BACK_TIMES, sendBackTimes.toString()); - message.sysHeaders().put(ProxyConstants.PROXY_SEND_BACK_IP, sendBackFromProxyIp); + message.getSystemProperties().put(ProxyConstants.PROXY_SEND_BACK_TIMES, sendBackTimes.toString()); + message.getSystemProperties().put(ProxyConstants.PROXY_SEND_BACK_IP, sendBackFromProxyIp); sendMsgBackToBroker(message, bizSeqNo); } } catch (Exception e){ logger.warn("handle msg exception when no session found", e); } - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_SUCCESS.name()); +// context.ack(); + context.commit(Action.CommitMessage); return; } @@ -607,18 +609,20 @@ public void onReceived(Message message, Context context) { if (session.isCanDownStream()) { session.downstreamMsg(downStreamMsgContext); - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + context.commit(Action.CommitMessage); return; } logger.warn("session is busy,dispatch retry,seq:{}, session:{}, bizSeq:{}", downStreamMsgContext.seq, downStreamMsgContext.session.getClient(), bizSeqNo); - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION)) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; + long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION)) ? 0 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; downStreamMsgContext.delay(delayTime); proxyTcpRetryer.pushRetry(downStreamMsgContext); - context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); - context.ack(); +// context.attributes().put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ProxyConsumeConcurrentlyStatus.CONSUME_FINISH.name()); +// context.ack(); + context.commit(Action.CommitMessage); } }; persistentMsgConsumer.subscribe(topic, listener); @@ -756,7 +760,7 @@ public MQConsumerWrapper getPersistentMsgConsumer() { private void sendMsgBackToBroker(Message msg, String bizSeqNo){ try { - String topic = msg.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); + String topic = msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); logger.warn("send msg back to broker, bizSeqno:{}, topic:{}",bizSeqNo, topic); send(new UpStreamMsgContext(null,null, msg), new SendCallback() { @@ -766,9 +770,14 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { logger.warn("consumerGroup:{} consume fail, sendMessageBack fail, bizSeqno:{}, topic:{}", groupName, bizSeqNo, topic); } + +// @Override +// public void onException(Throwable e) { +// logger.warn("consumerGroup:{} consume fail, sendMessageBack fail, bizSeqno:{}, topic:{}", groupName, bizSeqNo, topic); +// } }); proxyTcpMonitor.getProxy2mqMsgNum().incrementAndGet(); }catch (Exception e){ diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java index 7c72500778..a6fc6a5dd9 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/group/ClientSessionGroupMapping.java @@ -17,6 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.group; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.core.protocol.tcp.client.group.dispatch.FreePriorityDispatchStrategy; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.SessionState; @@ -32,7 +33,6 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.Message; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -288,7 +288,7 @@ private void handleUnackMsgsInSession(Session session){ if(unAckMsg.size() > 0 && session.getClientGroupWrapper().get().getGroupConsumerSessions().size() > 0){ for(Map.Entry entry : unAckMsg.entrySet()){ ClientAckContext ackContext = entry.getValue(); - if(ProxyUtil.isBroadcast(ackContext.getMsgs().get(0).sysHeaders().getString(Message.BuiltinKeys.DESTINATION))){ + if(ProxyUtil.isBroadcast(ackContext.getMsgs().get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION))){ logger.warn("exist broadcast msg unack when closeSession,seq:{},bizSeq:{},client:{}",ackContext.getSeq(),ProxyUtil.getMessageBizSeq(ackContext.getMsgs().get(0)),session.getClient()); continue; } @@ -372,7 +372,8 @@ public void run() { } tmp.getPusher().getPushContext().ackMsg(seqKey); tmp.getPusher().getPushContext().getUnAckMsg().remove(seqKey); - logger.warn("remove expire clientAckContext, session:{}, topic:{}, seq:{}", tmp, clientAckContext.getMsgs().get(0).sysHeaders().getString(Message.BuiltinKeys.DESTINATION), seqKey); + logger.warn("remove expire clientAckContext, session:{}, topic:{}, seq:{}", tmp, + clientAckContext.getMsgs().get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), seqKey); } } } @@ -380,7 +381,7 @@ public void run() { } private void initDownStreamMsgContextCleaner() { - proxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { + ProxyTCPServer.scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { Iterator cgwIterator = clientGroupMap.values().iterator(); @@ -394,7 +395,8 @@ public void run() { } cgw.getDownstreamMap().get(seq).ackMsg(); cgw.getDownstreamMap().remove(seq); - logger.warn("remove expire DownStreamMsgContext,group:{}, topic:{}, seq:{}", cgw.getGroupName(), downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION), seq); + logger.warn("remove expire DownStreamMsgContext,group:{}, topic:{}, seq:{}", cgw.getGroupName(), + downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), seq); } } } @@ -453,8 +455,9 @@ public HashMap statDCNSystemInfoByPurpose(String purpose) HashMap result = new HashMap(); if (!sessionTable.isEmpty()) { for (Session session : sessionTable.values()) { - if(!StringUtils.equals(session.getClient().getPurpose(), purpose)) + if(!StringUtils.equals(session.getClient().getPurpose(), purpose)){ continue; + } String key = session.getClient().getDcn() + "|" + session.getClient().getSubsystem()+ "|" + purpose; if (!result.containsKey(key)) { diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/Session.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/Session.java index ef9652a72b..45d652727f 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/Session.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/Session.java @@ -17,7 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push.SessionPusher; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.send.SessionSender; @@ -34,7 +34,8 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.Message; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -186,7 +187,7 @@ public void unsubscribe(List topics) throws Exception { } public ProxyTcpSendResult upstreamMsg(Header header, Message msg, SendCallback sendCallback, long startTime, long taskExecuteTime) { - String topic = msg.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); + String topic = msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); sessionContext.sendTopics.putIfAbsent(topic, topic); return sender.send(header, msg, sendCallback, startTime, taskExecuteTime); } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java index 3828f4ec45..dc7e5f9863 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/ClientAckContext.java @@ -18,10 +18,11 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push; import com.webank.eventmesh.api.AbstractContext; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.plugin.MQConsumerWrapper; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; @@ -51,7 +52,7 @@ public ClientAckContext(String seq, AbstractContext context, List msgs, this.msgs = msgs; this.consumer = consumer; this.createTime = System.currentTimeMillis(); - this.expireTime = System.currentTimeMillis() + Long.parseLong(msgs.get(0).userHeaders().getString(ProxyConstants.PROPERTY_MESSAGE_TTL)); + this.expireTime = System.currentTimeMillis() + Long.parseLong(msgs.get(0).getUserProperties(ProxyConstants.PROPERTY_MESSAGE_TTL)); } public boolean isExpire() { @@ -107,7 +108,7 @@ public void ackMsg() { consumer.updateOffset(msgs, context); // ConsumeMessageService consumeMessageService = consumer..getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgs, context); - logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).sysHeaders().getString(Message.BuiltinKeys.DESTINATION), ProxyUtil.getMessageBizSeq(msgs.get(0))); + logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), ProxyUtil.getMessageBizSeq(msgs.get(0))); }else{ logger.warn("ackMsg failed,consumer is null:{}, context is null:{} , msgs is null:{}",consumer == null, context == null, msgs == null); } @@ -119,7 +120,7 @@ public String toString() { ",seq=" + seq + // TODO ",consumer=" + consumer.getDefaultMQPushConsumer().getMessageModel() + // ",consumerGroup=" + consumer.getDefaultMQPushConsumer().getConsumerGroup() + - ",topic=" + (CollectionUtils.size(msgs) > 0 ? msgs.get(0).sysHeaders().getString(Message.BuiltinKeys.DESTINATION) : null) + + ",topic=" + (CollectionUtils.size(msgs) > 0 ? msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) : null) + ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + ",expireTime=" + DateFormatUtils.format(expireTime, ProxyConstants.DATE_FORMAT) + '}'; } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java index a105312672..58cda25275 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/DownStreamMsgContext.java @@ -18,11 +18,12 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push; import com.webank.eventmesh.api.AbstractContext; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.util.ServerGlobal; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.plugin.MQConsumerWrapper; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,7 +69,7 @@ public DownStreamMsgContext(Message msgExt, Session session, MQConsumerWrapper c this.lastPushTime = System.currentTimeMillis(); this.executeTime = System.currentTimeMillis(); this.createTime = System.currentTimeMillis(); - this.expireTime = System.currentTimeMillis() + Long.parseLong(msgExt.userHeaders().getString(ProxyConstants.TTL)); + this.expireTime = System.currentTimeMillis() + Long.parseLong(msgExt.getUserProperties("TTL")); this.msgFromOtherProxy = msgFromOtherProxy; } @@ -83,7 +84,8 @@ public void ackMsg() { consumer.updateOffset(msgs, consumeConcurrentlyContext); // ConsumeMessageService consumeMessageService = consumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgs, consumeConcurrentlyContext); - logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).sysHeaders().getString(Message.BuiltinKeys.DESTINATION), msgs.get(0).sysHeaders().getString(ProxyConstants.PROPERTY_MESSAGE_KEYS)); + logger.info("ackMsg topic:{}, bizSeq:{}", msgs.get(0).getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), + msgs.get(0).getSystemProperties(ProxyConstants.PROPERTY_MESSAGE_KEYS)); }else{ logger.warn("ackMsg failed,consumer is null:{}, context is null:{} , msgs is null:{}",consumer == null, consumeConcurrentlyContext == null, msgExt == null); } @@ -101,7 +103,7 @@ public String toString() { ",retryTimes=" + retryTimes + ",consumer=" + consumer + // todo ",consumerGroup=" + consumer.getClass().getConsumerGroup() + - ",topic=" + msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION) + + ",topic=" + msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) + ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + ",executeTime=" + DateFormatUtils.format(executeTime, ProxyConstants.DATE_FORMAT) + ",lastPushTime=" + DateFormatUtils.format(lastPushTime, ProxyConstants.DATE_FORMAT) + '}'; diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java index 0d5d837bdd..f8cbcf8571 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/PushContext.java @@ -20,7 +20,7 @@ import com.webank.eventmesh.api.AbstractContext; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.plugin.MQConsumerWrapper; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java index 7dcc970f77..b4e4ac171f 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/SessionPusher.java @@ -17,6 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; @@ -27,8 +28,7 @@ import com.webank.eventmesh.common.protocol.tcp.Package; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,21 +78,21 @@ public String toString() { public void push(final DownStreamMsgContext downStreamMsgContext) { Command cmd; - if (ProxyUtil.isBroadcast(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION))) { + if (ProxyUtil.isBroadcast(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION))) { cmd = Command.BROADCAST_MESSAGE_TO_CLIENT; - } else if (ProxyUtil.isService(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION))) { + } else if (ProxyUtil.isService(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION))) { cmd = Command.REQUEST_TO_CLIENT; } else { cmd = Command.ASYNC_MESSAGE_TO_CLIENT; } Package pkg = new Package(); - downStreamMsgContext.msgExt.sysHeaders().put(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + downStreamMsgContext.msgExt.getSystemProperties().put(ProxyConstants.REQ_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); AccessMessage body = null; int retCode = 0; String retMsg = null; try { - body = ProxyUtil.encodeMessage((BytesMessage) downStreamMsgContext.msgExt); + body = ProxyUtil.encodeMessage(downStreamMsgContext.msgExt); pkg.setBody(body); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, downStreamMsgContext.seq)); messageLogger.info("pkg|mq2proxy|cmd={}|mqMsg={}|user={}", cmd, ProxyUtil.printMqMessage(body), session.getClient()); @@ -129,7 +129,7 @@ public void operationComplete(ChannelFuture future) throws Exception { logger.warn("isolate client:{},isolateTime:{}", session.getClient(), isolateTime); //retry - long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION)) ? 0 : session.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; + long delayTime = ProxyUtil.isService(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION)) ? 0 : session.getAccessConfiguration().proxyTcpMsgRetryDelayInMills; downStreamMsgContext.delay(delayTime); session.getClientGroupWrapper().get().getProxyTcpRetryer().pushRetry(downStreamMsgContext); } else { diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java index 69c904015f..6a69a6c069 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/push/retry/ProxyTcpRetryer.java @@ -17,6 +17,7 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push.retry; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.util.ProxyThreadFactoryImpl; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.boot.ProxyTCPServer; @@ -25,7 +26,7 @@ import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.push.DownStreamMsgContext; //import com.webank.eventmesh.connector.defibus.common.Constants; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,7 +74,7 @@ public void pushRetry(DownStreamMsgContext downStreamMsgContext) { return; } - int maxRetryTimes = ProxyUtil.isService(downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION)) ? 1 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryTimes; + int maxRetryTimes = ProxyUtil.isService(downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION)) ? 1 : proxyTCPServer.getAccessConfiguration().proxyTcpMsgRetryTimes; if (downStreamMsgContext.retryTimes >= maxRetryTimes) { logger.warn("pushRetry fail,retry over maxRetryTimes:{}, retryTimes:{}, seq:{}, bizSeq:{}", maxRetryTimes, downStreamMsgContext.retryTimes, downStreamMsgContext.seq, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); @@ -109,14 +110,14 @@ private void retryHandle(DownStreamMsgContext downStreamMsgContext){ try { logger.info("retry downStream msg start,seq:{},retryTimes:{},bizSeq:{}",downStreamMsgContext.seq, downStreamMsgContext.retryTimes, ProxyUtil.getMessageBizSeq(downStreamMsgContext.msgExt)); - if(isRetryMsgTimeout(downStreamMsgContext)) + if(isRetryMsgTimeout(downStreamMsgContext)){ return; - + } downStreamMsgContext.retryTimes++; downStreamMsgContext.lastPushTime = System.currentTimeMillis(); Session rechoosen = null; - String topic = downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION); + String topic = downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION); if(!ProxyUtil.isBroadcast(topic)){ rechoosen = downStreamMsgContext.session.getClientGroupWrapper() .get().getDownstreamDispatchStrategy().select(downStreamMsgContext.session.getClientGroupWrapper().get().getGroupName() @@ -162,13 +163,13 @@ private void retryHandle(DownStreamMsgContext downStreamMsgContext){ private boolean isRetryMsgTimeout(DownStreamMsgContext downStreamMsgContext){ boolean flag =false; - long ttl = Long.parseLong(downStreamMsgContext.msgExt.userHeaders().getString(ProxyConstants.PROPERTY_MESSAGE_TTL)); + long ttl = Long.parseLong(downStreamMsgContext.msgExt.getUserProperties(ProxyConstants.PROPERTY_MESSAGE_TTL)); //TODO 关注是否能取到 - long storeTimestamp = Long.parseLong(downStreamMsgContext.msgExt.userHeaders().getString(DeFiBusConstant.STORE_TIME)); - String leaveTimeStr = downStreamMsgContext.msgExt.userHeaders().getString(DeFiBusConstant.LEAVE_TIME); + long storeTimestamp = Long.parseLong(downStreamMsgContext.msgExt.getUserProperties(DeFiBusConstant.STORE_TIME)); + String leaveTimeStr = downStreamMsgContext.msgExt.getUserProperties(DeFiBusConstant.LEAVE_TIME); long brokerCost = StringUtils.isNumeric(leaveTimeStr) ? Long.parseLong(leaveTimeStr) - storeTimestamp : 0; - String arriveTimeStr = downStreamMsgContext.msgExt.userHeaders().getString(DeFiBusConstant.ARRIVE_TIME); + String arriveTimeStr = downStreamMsgContext.msgExt.getUserProperties(DeFiBusConstant.ARRIVE_TIME); long accessCost = StringUtils.isNumeric(arriveTimeStr) ? System.currentTimeMillis() - Long.parseLong(arriveTimeStr) : 0; double elapseTime = brokerCost + accessCost; if (elapseTime >= ttl) { @@ -201,7 +202,8 @@ public int getRetrySize(){ private void proxyAckMsg(DownStreamMsgContext downStreamMsgContext){ List msgExts = new ArrayList(); msgExts.add(downStreamMsgContext.msgExt); - logger.warn("proxyAckMsg topic:{}, seq:{}, bizSeq:{}",downStreamMsgContext.msgExt.sysHeaders().getString(Message.BuiltinKeys.DESTINATION), downStreamMsgContext.seq, downStreamMsgContext.msgExt.sysHeaders().getString(ProxyConstants.PROPERTY_MESSAGE_KEYS)); + logger.warn("proxyAckMsg topic:{}, seq:{}, bizSeq:{}",downStreamMsgContext.msgExt.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION), + downStreamMsgContext.seq, downStreamMsgContext.msgExt.getSystemProperties(ProxyConstants.PROPERTY_MESSAGE_KEYS)); downStreamMsgContext.consumer.updateOffset(msgExts, downStreamMsgContext.consumeConcurrentlyContext); // ConsumeMessageService consumeMessageService = downStreamMsgContext.consumer.getDefaultMQPushConsumer().getDefaultMQPushConsumerImpl().getConsumeMessageService(); // ((ConsumeMessageConcurrentlyService)consumeMessageService).updateOffset(msgExts, downStreamMsgContext.consumeConcurrentlyContext); diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java index 8dafb49471..6f919cdbbd 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/SessionSender.java @@ -18,7 +18,7 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.send; import com.webank.eventmesh.api.RRCallback; -import com.webank.eventmesh.api.SendCallback; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.util.ProxyUtil; import com.webank.eventmesh.runtime.util.Utils; import com.webank.eventmesh.runtime.constants.DeFiBusConstant; @@ -28,8 +28,8 @@ import com.webank.eventmesh.common.protocol.tcp.Header; import com.webank.eventmesh.common.protocol.tcp.OPStatus; import com.webank.eventmesh.common.protocol.tcp.Package; -import io.openmessaging.BytesMessage; -import io.openmessaging.Message; +import io.openmessaging.api.Message; +import io.openmessaging.api.SendCallback; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; @@ -79,16 +79,15 @@ public ProxyTcpSendResult send(Header header, Message msg, SendCallback sendCall UpStreamMsgContext upStreamMsgContext = null; Command cmd = header.getCommand(); if (Command.REQUEST_TO_SERVER == cmd) { - long ttl = msg.sysHeaders().getString(DeFiBusConstant.PROPERTY_MESSAGE_TTL) != null ? Long.valueOf(msg.sysHeaders().getString - (DeFiBusConstant.PROPERTY_MESSAGE_TTL)) : ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; + long ttl = msg.getSystemProperties(DeFiBusConstant.PROPERTY_MESSAGE_TTL) != null ? Long.parseLong(msg.getSystemProperties(DeFiBusConstant.PROPERTY_MESSAGE_TTL)) : ProxyConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS; upStreamMsgContext = new UpStreamMsgContext(header.getSeq(), session, msg); session.getClientGroupWrapper().get().request(upStreamMsgContext, sendCallback, initSyncRRCallback(header, startTime, taskExecuteTime), ttl); } else if (Command.RESPONSE_TO_SERVER == cmd) { - String cluster = msg.userHeaders().getString(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER); + String cluster = msg.getUserProperties(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER); if (!StringUtils.isEmpty(cluster)) { String replyTopic = DeFiBusConstant.RR_REPLY_TOPIC; replyTopic = cluster + "-" + replyTopic; - msg.sysHeaders().put(Message.BuiltinKeys.DESTINATION, replyTopic); + msg.getSystemProperties().put(Constants.PROPERTY_MESSAGE_DESTINATION, replyTopic); } // //for rocketmq support @@ -133,8 +132,8 @@ public void onSuccess(Message msg) { // .getStoreTimestamp())); // } - msg.sysHeaders().put(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); - msg.sysHeaders().put(ProxyConstants.RSP_RECEIVE_PROXY_IP, session.getAccessConfiguration().proxyServerIp); + msg.getSystemProperties().put(ProxyConstants.RSP_MQ2PROXY_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + msg.getSystemProperties().put(ProxyConstants.RSP_RECEIVE_PROXY_IP, session.getAccessConfiguration().proxyServerIp); session.getClientGroupWrapper().get().getProxyTcpMonitor().getMq2proxyMsgNum().incrementAndGet(); Command cmd; @@ -146,9 +145,9 @@ public void onSuccess(Message msg) { } Package pkg = new Package(); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, seq)); - msg.sysHeaders().put(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); + msg.getSystemProperties().put(ProxyConstants.RSP_PROXY2C_TIMESTAMP, String.valueOf(System.currentTimeMillis())); try { - pkg.setBody(ProxyUtil.encodeMessage((BytesMessage) msg)); + pkg.setBody(ProxyUtil.encodeMessage(msg)); pkg.setHeader(new Header(cmd, OPStatus.SUCCESS.getCode(), null, seq)); } catch (Exception e) { pkg.setHeader(new Header(cmd, OPStatus.FAIL.getCode(), null, seq)); diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java index e5c5797d22..0603df6682 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/session/send/UpStreamMsgContext.java @@ -17,9 +17,10 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.session.send; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.Session; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.lang3.time.DateFormatUtils; public class UpStreamMsgContext { @@ -53,7 +54,7 @@ public long getCreateTime() { @Override public String toString() { return "UpStreamMsgContext{seq=" + seq - + ",topic=" + msg.sysHeaders().getString(Message.BuiltinKeys.DESTINATION) + + ",topic=" + msg.getSystemProperties(Constants.PROPERTY_MESSAGE_DESTINATION) + ",client=" + session.getClient() + ",createTime=" + DateFormatUtils.format(createTime, ProxyConstants.DATE_FORMAT) + "}"; } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java index 2df442eea9..b947179c83 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/core/protocol/tcp/client/task/MessageTransferTask.java @@ -17,7 +17,6 @@ package com.webank.eventmesh.runtime.core.protocol.tcp.client.task; -import com.webank.eventmesh.api.SendCallback; import com.webank.eventmesh.runtime.constants.DeFiBusConstant; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.send.ProxyTcpSendResult; import com.webank.eventmesh.runtime.core.protocol.tcp.client.session.send.ProxyTcpSendStatus; @@ -33,7 +32,9 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import io.openmessaging.producer.SendResult; +import io.openmessaging.api.OnExceptionContext; +import io.openmessaging.api.SendCallback; +import io.openmessaging.api.SendResult; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,15 +152,26 @@ public void onSuccess(SendResult sendResult) { } @Override - public void onException(Throwable e) { + public void onException(OnExceptionContext context) { session.getSender().getUpstreamBuff().release(); session.getSender().failMsgCount.incrementAndGet(); messageLogger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), String.valueOf - (System.currentTimeMillis() - createTime), new Exception(e)); - msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq())); + (System.currentTimeMillis() - createTime), new Exception(context.getException())); + msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), context.getException().toString(), pkg.getHeader().getSeq())); msg.setBody(accessMessage); Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); } + +// @Override +// public void onException(Throwable e) { +// session.getSender().getUpstreamBuff().release(); +// session.getSender().failMsgCount.incrementAndGet(); +// messageLogger.error("upstreamMsg mq message error|user={}|callback cost={}, errMsg={}", session.getClient(), String.valueOf +// (System.currentTimeMillis() - createTime), new Exception(e)); +// msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq())); +// msg.setBody(accessMessage); +// Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session); +// } }; } } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/MessageClientIDSetter.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/MessageClientIDSetter.java index 6b730d89dd..c71bdb54f9 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/MessageClientIDSetter.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/MessageClientIDSetter.java @@ -1,6 +1,6 @@ package com.webank.eventmesh.runtime.util; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; @@ -43,8 +43,8 @@ public class MessageClientIDSetter { } public static void setUniqID(final Message msg) { - if (msg.userHeaders().getString(PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) == null) { - msg.userHeaders().put(PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID()); + if (msg.getUserProperties(PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) == null) { + msg.getUserProperties().put(PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID()); } } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/OMSUtil.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/OMSUtil.java index 021ab4e6fe..bf2a27d266 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/OMSUtil.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/OMSUtil.java @@ -1,8 +1,8 @@ package com.webank.eventmesh.runtime.util; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; +import io.openmessaging.api.Message; +import io.openmessaging.api.OMSBuiltinKeys; import java.lang.reflect.Field; import java.util.HashMap; @@ -12,9 +12,9 @@ public class OMSUtil { public static boolean isOMSHeader(String value) { - for (Field field : Message.BuiltinKeys.class.getDeclaredFields()) { + for (Field field : OMSBuiltinKeys.class.getDeclaredFields()) { try { - if (field.get(Message.BuiltinKeys.class).equals(value)) { + if (field.get(OMSBuiltinKeys.class).equals(value)) { return true; } } catch (IllegalAccessException e) { @@ -24,13 +24,13 @@ public static boolean isOMSHeader(String value) { return false; } - public static Properties convertKeyValue2Prop(KeyValue keyValue){ - Properties properties = new Properties(); - for (String key : keyValue.keySet()){ - properties.put(key, keyValue.getString(key)); - } - return properties; - } +// public static Properties convertKeyValue2Prop(KeyValue keyValue){ +// Properties properties = new Properties(); +// for (String key : keyValue.keySet()){ +// properties.put(key, keyValue.getString(key)); +// } +// return properties; +// } public static Map combineProp(Properties p1, Properties p2){ Properties properties = new Properties(); @@ -41,8 +41,8 @@ public static Map combineProp(Properties p1, Properties p2){ } public static Map getMessageProp(Message message){ - Properties p1 = convertKeyValue2Prop(message.sysHeaders()); - Properties p2 = convertKeyValue2Prop(message.userHeaders()); + Properties p1 = message.getSystemProperties(); + Properties p2 = message.getUserProperties(); return combineProp(p1, p2); } diff --git a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/ProxyUtil.java b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/ProxyUtil.java index 66fe949d56..662ada7431 100644 --- a/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/ProxyUtil.java +++ b/eventmesh-runtime/src/main/java/com/webank/eventmesh/runtime/util/ProxyUtil.java @@ -18,6 +18,7 @@ package com.webank.eventmesh.runtime.util; +import com.webank.eventmesh.common.Constants; import com.webank.eventmesh.runtime.constants.ProxyConstants; import com.webank.eventmesh.runtime.constants.ProxyVersion; import com.webank.eventmesh.common.ThreadUtil; @@ -27,10 +28,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import com.webank.eventmesh.runtime.domain.BytesMessageImpl; -import io.openmessaging.BytesMessage; -import io.openmessaging.KeyValue; -import io.openmessaging.Message; +import io.openmessaging.api.Message; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; @@ -42,6 +40,7 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.*; import static com.webank.eventmesh.runtime.util.OMSUtil.isOMSHeader; @@ -225,11 +224,11 @@ public static String printMqMessage(AccessMessage accessMessage) { } public static String getMessageBizSeq(Message msg) { - KeyValue properties = msg.sysHeaders(); + Properties properties = msg.getSystemProperties(); - String keys = properties.getString(ProxyConstants.KEYS_UPPERCASE); + String keys = properties.getProperty(ProxyConstants.KEYS_UPPERCASE); if (!StringUtils.isNotBlank(keys)) { - keys = properties.getString(ProxyConstants.KEYS_LOWERCASE); + keys = properties.getProperty(ProxyConstants.KEYS_LOWERCASE); } return keys; } @@ -245,24 +244,26 @@ public static String getMessageBizSeq(Message msg) { // return msg; // } - public static BytesMessage decodeMessage(AccessMessage accessMessage) { - BytesMessage omsMsg = new BytesMessageImpl(); + public static Message decodeMessage(AccessMessage accessMessage) { + Message omsMsg = new Message(); omsMsg.setBody(accessMessage.getBody().getBytes()); - - KeyValue headers = omsMsg.sysHeaders(); - KeyValue properties = omsMsg.userHeaders(); + omsMsg.setTopic(accessMessage.getTopic()); + Properties systemProperties = new Properties(); + Properties userProperties = new Properties(); final Set> entries = accessMessage.getProperties().entrySet(); for (final Map.Entry entry : entries) { if (isOMSHeader(entry.getKey())) { - headers.put(entry.getKey(), entry.getValue()); + systemProperties.put(entry.getKey(), entry.getValue()); } else { - properties.put(entry.getKey(), entry.getValue()); + userProperties.put(entry.getKey(), entry.getValue()); } } - omsMsg.putSysHeaders(Message.BuiltinKeys.DESTINATION, accessMessage.getTopic()); + systemProperties.put(Constants.PROPERTY_MESSAGE_DESTINATION, accessMessage.getTopic()); + omsMsg.setSystemProperties(systemProperties); + omsMsg.setUserProperties(userProperties); return omsMsg; } @@ -276,32 +277,38 @@ public static BytesMessage decodeMessage(AccessMessage accessMessage) { // return accessMessage; // } - public static AccessMessage encodeMessage(BytesMessage omsMessage) throws Exception { + public static AccessMessage encodeMessage(Message omsMessage) throws Exception { AccessMessage accessMessage = new AccessMessage(); - accessMessage.setBody(new String(omsMessage.getBody(byte[].class), "UTF-8")); + accessMessage.setBody(new String(omsMessage.getBody(), StandardCharsets.UTF_8)); - KeyValue sysHeaders = omsMessage.sysHeaders(); - KeyValue userHeaders = omsMessage.userHeaders(); + Properties sysHeaders = omsMessage.getSystemProperties(); + Properties userHeaders = omsMessage.getUserProperties(); //All destinations in RocketMQ use Topic - accessMessage.setTopic(sysHeaders.getString(Message.BuiltinKeys.DESTINATION)); + accessMessage.setTopic(sysHeaders.getProperty(Constants.PROPERTY_MESSAGE_DESTINATION)); + + if (sysHeaders.containsKey("START_TIME")) { + long deliverTime; + if (StringUtils.isBlank(sysHeaders.getProperty("START_TIME"))){ + deliverTime = 0; + }else { + deliverTime = Long.parseLong(sysHeaders.getProperty("START_TIME")); + } - if (sysHeaders.containsKey(Message.BuiltinKeys.START_TIME)) { - long deliverTime = sysHeaders.getLong(Message.BuiltinKeys.START_TIME, 0); if (deliverTime > 0) { // rmqMessage.putUserProperty(RocketMQConstants.START_DELIVER_TIME, String.valueOf(deliverTime)); - accessMessage.getProperties().put(Message.BuiltinKeys.START_TIME, String.valueOf(deliverTime)); + accessMessage.getProperties().put("START_TIME", String.valueOf(deliverTime)); } } - for (String key : userHeaders.keySet()) { - accessMessage.getProperties().put(key, userHeaders.getString(key)); + for (String key : userHeaders.stringPropertyNames()) { + accessMessage.getProperties().put(key, userHeaders.getProperty(key)); } //System headers has a high priority - for (String key : sysHeaders.keySet()) { - accessMessage.getProperties().put(key, sysHeaders.getString(key)); + for (String key : sysHeaders.stringPropertyNames()) { + accessMessage.getProperties().put(key, sysHeaders.getProperty(key)); } return accessMessage;