-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connector support cloud event #586
Merged
xwm1992
merged 1 commit into
apache:cloudevents
from
wangshaojie4039:connector-rocketmq-cloudevent
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
.../main/java/org/apache/eventmesh/connector/rocketmq/cloudevent/RocketMQMessageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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 org.apache.eventmesh.connector.rocketmq.cloudevent; | ||
|
||
import org.apache.rocketmq.common.message.Message; | ||
|
||
import java.util.Map; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import io.cloudevents.core.message.MessageReader; | ||
import io.cloudevents.core.message.MessageWriter; | ||
import io.cloudevents.core.message.impl.GenericStructuredMessageReader; | ||
import io.cloudevents.core.message.impl.MessageUtils; | ||
import io.cloudevents.lang.Nullable; | ||
import io.cloudevents.rw.CloudEventRWException; | ||
import io.cloudevents.rw.CloudEventWriter; | ||
|
||
import org.apache.eventmesh.connector.rocketmq.cloudevent.impl.RocketMQBinaryMessageReader; | ||
import org.apache.eventmesh.connector.rocketmq.cloudevent.impl.RocketMQHeaders; | ||
import org.apache.eventmesh.connector.rocketmq.cloudevent.impl.RocketMQMessageWriter; | ||
|
||
|
||
@ParametersAreNonnullByDefault | ||
public final class RocketMQMessageFactory { | ||
|
||
private RocketMQMessageFactory() { | ||
// prevent instantiation | ||
} | ||
|
||
public static MessageReader createReader(final Message message) throws CloudEventRWException { | ||
return createReader(message.getProperties(), message.getBody()); | ||
} | ||
|
||
|
||
public static MessageReader createReader(final Map<String, String> props, | ||
@Nullable final byte[] body) | ||
throws CloudEventRWException { | ||
|
||
return MessageUtils.parseStructuredOrBinaryMessage( | ||
() -> props.get(RocketMQHeaders.CONTENT_TYPE), | ||
format -> new GenericStructuredMessageReader(format, body), | ||
() -> props.get(RocketMQHeaders.SPEC_VERSION), | ||
sv -> new RocketMQBinaryMessageReader(sv, props, body) | ||
); | ||
} | ||
|
||
|
||
public static MessageWriter<CloudEventWriter<Message>, Message> createWriter(String topic) { | ||
return new RocketMQMessageWriter<>(topic); | ||
} | ||
|
||
public static MessageWriter<CloudEventWriter<Message>, Message> createWriter(String topic, | ||
String keys) { | ||
return new RocketMQMessageWriter<>(topic, keys); | ||
} | ||
|
||
public static MessageWriter<CloudEventWriter<Message>, Message> createWriter(String topic, | ||
String keys, | ||
String tags) { | ||
return new RocketMQMessageWriter<>(topic, keys, tags); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
.../org/apache/eventmesh/connector/rocketmq/cloudevent/impl/RocketMQBinaryMessageReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 org.apache.eventmesh.connector.rocketmq.cloudevent.impl; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
|
||
import io.cloudevents.SpecVersion; | ||
import io.cloudevents.core.data.BytesCloudEventData; | ||
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl; | ||
|
||
public class RocketMQBinaryMessageReader | ||
extends BaseGenericBinaryMessageReaderImpl<String, String> { | ||
|
||
private final Map<String, String> headers; | ||
|
||
public RocketMQBinaryMessageReader(SpecVersion version, Map<String, String> headers, | ||
byte[] payload) { | ||
super(version, | ||
payload != null && payload.length > 0 ? BytesCloudEventData.wrap(payload) : null); | ||
|
||
Objects.requireNonNull(headers); | ||
this.headers = headers; | ||
} | ||
|
||
@Override | ||
protected boolean isContentTypeHeader(String key) { | ||
return key.equals(RocketMQHeaders.CONTENT_TYPE); | ||
} | ||
|
||
@Override | ||
protected boolean isCloudEventsHeader(String key) { | ||
return key.length() > 3 && key.substring(0, RocketMQHeaders.CE_PREFIX.length()) | ||
.startsWith(RocketMQHeaders.CE_PREFIX); | ||
} | ||
|
||
@Override | ||
protected String toCloudEventsKey(String key) { | ||
return key.substring(RocketMQHeaders.CE_PREFIX.length()).toLowerCase(); | ||
} | ||
|
||
@Override | ||
protected void forEachHeader(BiConsumer<String, String> fn) { | ||
this.headers.forEach((k, v) -> fn.accept(k, v)); | ||
} | ||
|
||
@Override | ||
protected String toCloudEventsValue(String value) { | ||
return value; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...rc/main/java/org/apache/eventmesh/connector/rocketmq/cloudevent/impl/RocketMQHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 org.apache.eventmesh.connector.rocketmq.cloudevent.impl; | ||
|
||
import java.util.Map; | ||
|
||
import io.cloudevents.core.message.impl.MessageUtils; | ||
import io.cloudevents.core.v1.CloudEventV1; | ||
|
||
public class RocketMQHeaders { | ||
|
||
public static final String CE_PREFIX = "CE_"; | ||
|
||
protected static final Map<String, String> ATTRIBUTES_TO_HEADERS = | ||
MessageUtils.generateAttributesToHeadersMapping(v -> CE_PREFIX + v); | ||
|
||
public static final String CONTENT_TYPE = | ||
ATTRIBUTES_TO_HEADERS.get(CloudEventV1.DATACONTENTTYPE); | ||
|
||
public static final String SPEC_VERSION = ATTRIBUTES_TO_HEADERS.get(CloudEventV1.SPECVERSION); | ||
|
||
|
||
} | ||
|
104 changes: 104 additions & 0 deletions
104
...n/java/org/apache/eventmesh/connector/rocketmq/cloudevent/impl/RocketMQMessageWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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 org.apache.eventmesh.connector.rocketmq.cloudevent.impl; | ||
|
||
import org.apache.rocketmq.common.message.Message; | ||
|
||
import io.cloudevents.CloudEventData; | ||
import io.cloudevents.SpecVersion; | ||
import io.cloudevents.core.format.EventFormat; | ||
import io.cloudevents.core.message.MessageWriter; | ||
import io.cloudevents.rw.CloudEventContextWriter; | ||
import io.cloudevents.rw.CloudEventRWException; | ||
import io.cloudevents.rw.CloudEventWriter; | ||
|
||
|
||
public final class RocketMQMessageWriter<R> | ||
implements MessageWriter<CloudEventWriter<Message>, Message>, CloudEventWriter<Message> { | ||
|
||
private Message message; | ||
|
||
|
||
public RocketMQMessageWriter(String topic) { | ||
message = new Message(); | ||
message.setTopic(topic); | ||
} | ||
|
||
public RocketMQMessageWriter(String topic, String keys) { | ||
message = new Message(); | ||
|
||
message.setTopic(topic); | ||
|
||
if (keys != null && keys.length() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
message.setKeys(keys); | ||
} | ||
} | ||
|
||
public RocketMQMessageWriter(String topic, String keys, String tags) { | ||
message = new Message(); | ||
|
||
message.setTopic(topic); | ||
|
||
if (tags != null && tags.length() > 0) { | ||
message.setTags(tags); | ||
} | ||
|
||
if (keys != null && keys.length() > 0) { | ||
message.setKeys(keys); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public CloudEventContextWriter withContextAttribute(String name, String value) | ||
throws CloudEventRWException { | ||
|
||
String propName = RocketMQHeaders.ATTRIBUTES_TO_HEADERS.get(name); | ||
if (propName == null) { | ||
propName = RocketMQHeaders.CE_PREFIX + name; | ||
} | ||
message.putUserProperty(propName, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public RocketMQMessageWriter<R> create(final SpecVersion version) { | ||
message.putUserProperty(RocketMQHeaders.SPEC_VERSION, version.toString()); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Message setEvent(final EventFormat format, final byte[] value) | ||
throws CloudEventRWException { | ||
message.putUserProperty(RocketMQHeaders.CONTENT_TYPE, format.serializedContentType()); | ||
message.setBody(value); | ||
return message; | ||
} | ||
|
||
@Override | ||
public Message end(final CloudEventData data) throws CloudEventRWException { | ||
message.setBody(data.toBytes()); | ||
return message; | ||
} | ||
|
||
@Override | ||
public Message end() { | ||
message.setBody(null); | ||
return message; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class will be removed because OMS will be removed. This part is modified to avoid error reporting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this change keep the server start success?