-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andreas Mautsch
committed
Jul 5, 2024
1 parent
47f8731
commit 4f03f07
Showing
5 changed files
with
62 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
group=org.goafabric | ||
version=3.3.1-SNAPSHOT | ||
version=3.3.1-aspect-SNAPSHOT |
14 changes: 14 additions & 0 deletions
14
src/main/java/org/goafabric/eventdispatcher/consumer/nats/NatsListener.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,14 @@ | ||
package org.goafabric.eventdispatcher.consumer.nats; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface NatsListener { | ||
String consumerName(); | ||
String subject(); | ||
} | ||
|
42 changes: 42 additions & 0 deletions
42
src/main/java/org/goafabric/eventdispatcher/consumer/nats/NatsListenerAspect.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,42 @@ | ||
package org.goafabric.eventdispatcher.consumer.nats; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Component | ||
public class NatsListenerAspect { | ||
|
||
private final ConsumerUtil consumerUtil; | ||
|
||
@Autowired | ||
public NatsListenerAspect(ConsumerUtil consumerUtil) { | ||
this.consumerUtil = consumerUtil; | ||
} | ||
|
||
@Pointcut("@annotation(org.goafabric.eventdispatcher.consumer.nats.NatsListener)") | ||
public void natsListenerAnnotation() {} | ||
|
||
@Around("natsListenerAnnotation()") | ||
public Object invokeNatsListener(ProceedingJoinPoint joinPoint, NatsListener natsListener) throws Throwable { | ||
String consumerName = natsListener.consumerName(); | ||
String subject = natsListener.subject(); | ||
|
||
// Subscribe using ConsumerUtil | ||
consumerUtil.subscribe(consumerName, subject, (msg, eventData) -> { | ||
try { | ||
// Invoke the annotated method | ||
joinPoint.proceed(new Object[]{msg, eventData}); | ||
} catch (Throwable throwable) { | ||
throw new RuntimeException("Error invoking annotated method", throwable); | ||
} | ||
}); | ||
|
||
// Return null as we are handling the method execution manually | ||
return null; | ||
} | ||
} |
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