Skip to content

Commit

Permalink
GH-248 - Initial prototype to automatically externalize events.
Browse files Browse the repository at this point in the history
We now provide an SPI to plug custom implementations to externalize events that match an EventExternalizationConfiguration (EEC) into messaging technology. The first implementation of that API is based on Spring Kafka to consume a KafkaTemplate and translate events into Kafka messages.
  • Loading branch information
odrotbohm committed Aug 7, 2023
1 parent 13f0cde commit a204610
Show file tree
Hide file tree
Showing 33 changed files with 1,691 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 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.modulith;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

/**
* Marks domain events as to be externalized.
*
* @author Oliver Drotbohm
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Externalized {

/**
* The logical target name. Will default to a strategy defined by configuration if empty.
*
* @return
* @see #target()
*/
@AliasFor("target")
String value() default "";

/**
* The logical target name. Will default to a strategy defined by configuration if empty.
*
* @return
* @see #value()
*/
@AliasFor("value")
String target() default "";
}
2 changes: 2 additions & 0 deletions spring-modulith-events/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
<name>Spring Modulith - Events</name>

<modules>
<module>spring-modulith-events-api</module>
<module>spring-modulith-events-core</module>
<module>spring-modulith-events-jpa</module>
<module>spring-modulith-events-jdbc</module>
<module>spring-modulith-events-mongodb</module>
<module>spring-modulith-events-jackson</module>
<module>spring-modulith-events-kafka</module>
</modules>

<profiles>
Expand Down
47 changes: 47 additions & 0 deletions spring-modulith-events/spring-modulith-events-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<name>Spring Modulith - Events - API</name>
<artifactId>spring-modulith-events-api</artifactId>

<properties>
<module.name>org.springframework.modulith.events.api</module.name>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>

<dependency>
<groupId>org.jmolecules</groupId>
<artifactId>jmolecules-events</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright 2023 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.modulith.events.externalize;

import static org.springframework.core.annotation.AnnotatedElementUtils.*;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.springframework.modulith.Externalized;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;

/**
* An annotation based target lookup strategy to enable caching of the function lookups that involve classpath checks.
* The currently supported annotations are:
* <ul>
* <li>Spring Modulith's {@link Externalized}</li>
* <li>jMolecules {@link org.jmolecules.event.annotation.Externalized} (if present on the classpath)</li>
* </ul>
*
* @author Oliver Drotbohm
* @since 1.1
*/
class AnnotationTargetLookup implements Supplier<Optional<String>> {

private static Map<Class<?>, AnnotationTargetLookup> LOOKUPS = new ConcurrentReferenceHashMap<>(25);
private static final String JMOLECULES_EXTERNALIZED = "org.jmolecules.event.annotation.Externalized";
private static final Class<? extends Annotation> JMOLECULES_ANNOTATION = loadJMoleculesExternalizedIfPresent();

static {

}

private final Class<?> type;
private final Supplier<Optional<String>> lookup;

/**
* Creates a new {@link AnnotationTargetLookup} for the given type.
*
* @param type must not be {@literal null}.
*/
private AnnotationTargetLookup(Class<?> type) {

Assert.notNull(type, "Type must not be null!");

this.type = type;
this.lookup = firstMatching(fromJMoleculesExternalized(), fromModulithExternalized());
}

/**
* Returns the {@link AnnotationTargetLookup} for the given type.
*
* @param type must not be {@literal null}.
* @return will never be {@literal null}.
*/
static AnnotationTargetLookup of(Class<?> type) {
return LOOKUPS.computeIfAbsent(type, AnnotationTargetLookup::new);
}

static boolean hasExternalizedAnnotation(Object event) {

var type = event.getClass();

return hasAnnotation(type, Externalized.class)
|| JMOLECULES_ANNOTATION != null && hasAnnotation(type, JMOLECULES_ANNOTATION);
}

/*
* (non-Javadoc)
* @see java.util.function.Supplier#get()
*/
@Override
public Optional<String> get() {
return lookup.get();
}

/**
* Creates a {@link Supplier} to lookup the target from Spring Modulith's {@link Externalized} annotation.
*
* @return will never be {@literal null}.
*/
private Supplier<Optional<String>> fromModulithExternalized() {
return () -> lookupTarget(Externalized.class, Externalized::target);
}

/**
* Creates a {@link Supplier} to lookup the target from jMolecules
* {@link org.jmolecules.event.annotation.Externalized} annotation if present on the classpath.
*
* @return will never be {@literal null}.
*/
private Supplier<Optional<String>> fromJMoleculesExternalized() {

return JMOLECULES_ANNOTATION == null
? () -> Optional.empty()
: () -> lookupTarget(org.jmolecules.event.annotation.Externalized.class,
org.jmolecules.event.annotation.Externalized::target,
org.jmolecules.event.annotation.Externalized::value);
}

/**
* Returns a new {@link Function} that chains the given lookup functions until one returns a non-empty
* {@link Optional}.
*
* @param functions must not be {@literal null}.
* @return will never be {@literal null}.
*/
@SafeVarargs
private Supplier<Optional<String>> firstMatching(
Supplier<Optional<String>>... functions) {

return () -> Arrays.stream(functions)
.reduce(Optional.empty(), (current, function) -> current.or(() -> function.get()), (l, r) -> r);
}

/**
* Looks up the target from the given annotation applying the given extractors aborting if a non-empty {@link String}
* is found.
*
* @param <T> the annotation type
* @param annotation must not be {@literal null}.
* @param extractors must not be {@literal null}.
* @return will never be {@literal null}.
*/
@SafeVarargs
private <T extends Annotation> Optional<String> lookupTarget(Class<T> annotation,
Function<T, String>... extractors) {

return Optional.ofNullable(findMergedAnnotation(type, annotation))
.stream()
.flatMap(it -> Arrays.stream(extractors)
.map(function -> function.apply(it))
.filter(Predicate.not(String::isBlank)))
.findFirst();
}

private static Class<? extends Annotation> loadJMoleculesExternalizedIfPresent() {

var classLoader = DefaultEventExternalizationConfiguration.class.getClassLoader();

if (ClassUtils.isPresent(JMOLECULES_EXTERNALIZED, classLoader)) {

try {
return (Class<? extends Annotation>) ClassUtils.forName(JMOLECULES_EXTERNALIZED, classLoader);
} catch (Exception o_O) {
return null;
}
}

return null;
}
}
Loading

0 comments on commit a204610

Please sign in to comment.