This repository was archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 368
Adding support for EventBridge #525
Open
jmnarloch
wants to merge
1
commit into
spring-attic:main
Choose a base branch
from
jmnarloch:eventbridge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
...ain/java/org/springframework/cloud/aws/messaging/config/annotation/EnableEventBridge.java
This file contains hidden or 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,35 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.cloud.aws.messaging.config.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* @author Jakub Narloch | ||
* @since 2.3.0 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Import({ EventBridgeConfiguration.class }) | ||
public @interface EnableEventBridge { | ||
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. Spring Cloud has moved away from |
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...a/org/springframework/cloud/aws/messaging/config/annotation/EventBridgeConfiguration.java
This file contains hidden or 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,50 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.cloud.aws.messaging.config.annotation; | ||
|
||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; | ||
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClient; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.aws.context.annotation.ConditionalOnMissingAmazonClient; | ||
import org.springframework.cloud.aws.core.config.AmazonWebserviceClientFactoryBean; | ||
import org.springframework.cloud.aws.core.region.RegionProvider; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Jakub Narloch | ||
* @since 2.3.0 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
public class EventBridgeConfiguration { | ||
|
||
@Autowired(required = false) | ||
private AWSCredentialsProvider awsCredentialsProvider; | ||
|
||
@Autowired(required = false) | ||
private RegionProvider regionProvider; | ||
|
||
@ConditionalOnMissingAmazonClient(AmazonCloudWatchEvents.class) | ||
@Bean | ||
public AmazonWebserviceClientFactoryBean<AmazonCloudWatchEventsClient> amazonEvents() { | ||
return new AmazonWebserviceClientFactoryBean<>(AmazonCloudWatchEventsClient.class, | ||
this.awsCredentialsProvider, this.regionProvider); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...ng/src/main/java/org/springframework/cloud/aws/messaging/core/EventBusMessageChannel.java
This file contains hidden or 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,76 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.cloud.aws.messaging.core; | ||
|
||
import java.util.Optional; | ||
|
||
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; | ||
import com.amazonaws.services.cloudwatchevents.model.PutEventsRequest; | ||
import com.amazonaws.services.cloudwatchevents.model.PutEventsRequestEntry; | ||
|
||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.support.AbstractMessageChannel; | ||
|
||
/** | ||
* @author Jakub Narloch | ||
* @since 2.3.0 | ||
*/ | ||
public class EventBusMessageChannel extends AbstractMessageChannel { | ||
|
||
/** | ||
* The 'source' message header. | ||
*/ | ||
public static final String EVENT_SOURCE_HEADER = "EVENT_SOURCE_HEADER"; | ||
|
||
/** | ||
* The 'detail-type' message header. | ||
*/ | ||
public static final String EVENT_DETAIL_TYPE_HEADER = "EVENT_DETAIL_TYPE_HEADER"; | ||
|
||
private final AmazonCloudWatchEvents amazonEvents; | ||
|
||
private final String eventBus; | ||
|
||
public EventBusMessageChannel(AmazonCloudWatchEvents amazonEvents, String eventBus) { | ||
this.amazonEvents = amazonEvents; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
@Override | ||
protected boolean sendInternal(Message<?> message, long timeout) { | ||
PutEventsRequestEntry entry = new PutEventsRequestEntry() | ||
.withEventBusName(eventBus).withSource(findEventSource(message)) | ||
.withDetailType(findEventDetailType(message)) | ||
.withDetail(message.getPayload().toString()); | ||
amazonEvents.putEvents(new PutEventsRequest().withEntries(entry)); | ||
return true; | ||
} | ||
|
||
private static String findEventSource(Message<?> message) { | ||
return findHeaderValue(message, EVENT_SOURCE_HEADER); | ||
} | ||
|
||
private static String findEventDetailType(Message<?> message) { | ||
return findHeaderValue(message, EVENT_DETAIL_TYPE_HEADER); | ||
} | ||
|
||
private static String findHeaderValue(Message<?> message, String header) { | ||
return Optional.ofNullable(message.getHeaders().get(header)).map(Object::toString) | ||
.orElse(null); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
...g/src/main/java/org/springframework/cloud/aws/messaging/core/EventsMessagingTemplate.java
This file contains hidden or 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,96 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.cloud.aws.messaging.core; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; | ||
|
||
import org.springframework.cloud.aws.core.env.ResourceIdResolver; | ||
import org.springframework.cloud.aws.messaging.core.support.AbstractMessageChannelMessagingSendingTemplate; | ||
import org.springframework.cloud.aws.messaging.support.destination.DynamicEventBusDestinationResolver; | ||
import org.springframework.messaging.converter.MessageConverter; | ||
import org.springframework.messaging.core.DestinationResolver; | ||
|
||
/** | ||
* @author Jakub Narloch | ||
* @since 2.3.0 | ||
*/ | ||
public class EventsMessagingTemplate | ||
extends AbstractMessageChannelMessagingSendingTemplate<EventBusMessageChannel> { | ||
|
||
private final AmazonCloudWatchEvents amazonEvents; | ||
|
||
public EventsMessagingTemplate(AmazonCloudWatchEvents amazonEvents) { | ||
this(amazonEvents, (ResourceIdResolver) null, null); | ||
} | ||
|
||
public EventsMessagingTemplate(AmazonCloudWatchEvents amazonEvents, | ||
ResourceIdResolver resourceIdResolver, MessageConverter messageConverter) { | ||
super(new DynamicEventBusDestinationResolver(amazonEvents, resourceIdResolver)); | ||
this.amazonEvents = amazonEvents; | ||
initMessageConverter(messageConverter); | ||
} | ||
|
||
public EventsMessagingTemplate(AmazonCloudWatchEvents amazonEvents, | ||
DestinationResolver<String> destinationResolver, | ||
MessageConverter messageConverter) { | ||
super(destinationResolver); | ||
this.amazonEvents = amazonEvents; | ||
initMessageConverter(messageConverter); | ||
} | ||
|
||
@Override | ||
protected EventBusMessageChannel resolveMessageChannel( | ||
String physicalResourceIdentifier) { | ||
return new EventBusMessageChannel(this.amazonEvents, physicalResourceIdentifier); | ||
} | ||
|
||
/** | ||
* Convenience method that sends an event identified by {@literal source} and | ||
* {@literal detailType} with the given {@literal message} to the | ||
* {@literal destination}. | ||
* @param source The event source | ||
* @param detailType The event detail-type | ||
* @param message The event body to send | ||
*/ | ||
public void sendEvent(String source, String detailType, Object message) { | ||
Map<String, Object> headers = new HashMap<>(); | ||
headers.put(EventBusMessageChannel.EVENT_SOURCE_HEADER, source); | ||
headers.put(EventBusMessageChannel.EVENT_DETAIL_TYPE_HEADER, detailType); | ||
this.convertAndSend(getRequiredDefaultDestination(), message, headers); | ||
} | ||
|
||
/** | ||
* Convenience method that sends an event identified by {@literal source} and | ||
* {@literal detailType} with the given {@literal message} to the specific | ||
* {@literal eventBus}. | ||
* @param eventBus The event bus name | ||
* @param source The event source | ||
* @param detailType The event detail-type | ||
* @param message The event body to send | ||
*/ | ||
public void sendEvent(String eventBus, String source, String detailType, | ||
Object message) { | ||
Map<String, Object> headers = new HashMap<>(); | ||
headers.put(EventBusMessageChannel.EVENT_SOURCE_HEADER, source); | ||
headers.put(EventBusMessageChannel.EVENT_DETAIL_TYPE_HEADER, detailType); | ||
this.convertAndSend(eventBus, message, headers); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...framework/cloud/aws/messaging/support/destination/DynamicEventBusDestinationResolver.java
This file contains hidden or 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,74 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.cloud.aws.messaging.support.destination; | ||
|
||
import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; | ||
import com.amazonaws.services.cloudwatchevents.model.CreateEventBusRequest; | ||
|
||
import org.springframework.cloud.aws.core.env.ResourceIdResolver; | ||
import org.springframework.cloud.aws.core.naming.AmazonResourceName; | ||
import org.springframework.messaging.core.DestinationResolutionException; | ||
import org.springframework.messaging.core.DestinationResolver; | ||
|
||
/** | ||
* @author Jakub Narloch | ||
* @since 2.3.0 | ||
*/ | ||
public class DynamicEventBusDestinationResolver implements DestinationResolver<String> { | ||
|
||
private final AmazonCloudWatchEvents amazonEvents; | ||
|
||
private final ResourceIdResolver resourceIdResolver; | ||
|
||
private boolean autoCreate; | ||
|
||
public DynamicEventBusDestinationResolver(AmazonCloudWatchEvents amazonEvents) { | ||
this(amazonEvents, null); | ||
} | ||
|
||
public DynamicEventBusDestinationResolver(AmazonCloudWatchEvents amazonEvents, | ||
ResourceIdResolver resourceIdResolver) { | ||
this.amazonEvents = amazonEvents; | ||
this.resourceIdResolver = resourceIdResolver; | ||
} | ||
|
||
public void setAutoCreate(boolean autoCreate) { | ||
this.autoCreate = autoCreate; | ||
} | ||
|
||
@Override | ||
public String resolveDestination(String name) throws DestinationResolutionException { | ||
if (autoCreate) { | ||
amazonEvents.createEventBus(new CreateEventBusRequest().withName(name)) | ||
.getEventBusArn(); | ||
return name; | ||
} | ||
|
||
String eventBusName = name; | ||
if (resourceIdResolver != null) { | ||
eventBusName = resourceIdResolver.resolveToPhysicalResourceId(name); | ||
} | ||
|
||
if (eventBusName != null | ||
&& AmazonResourceName.isValidAmazonResourceName(eventBusName)) { | ||
return AmazonResourceName.fromString(eventBusName).getResourceName(); | ||
} | ||
|
||
return eventBusName; | ||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.