Given a service Sender with properties, and an aspect service LogSender, how does LogSender get the service properties of the current Sender? I'd like to add a property to optionally log the data that a specific Sender is sending.
component.getServiceProperties(); seems to return the LogSender's service properties instead of Sender's properties.
I've looked at ConfigAdmin but I don't see a way to associate the Sender that LogSender aspected, with the specific configuration used.
I'm using Apache Felix as my OSGi container if that's relevant.
Here's the Activator's init method after adding ConfigurationAdmin to the dependency list.
public void init(BundleContext context, DependencyManager manager) throws Exception {
manager.add(createAspectService(Sender.class, null, 10).setImplementation(LogSender.class)
.add(createServiceDependency().setService(ConfigurationAdmin.class)
.setRequired(true)));
.add(createServiceDependency().setService(LogService.class).setRequired(true)));
}
To inject the service properties of the original Sender into the LogSender aspect, you can use the signature in DependencyActivatorBase (or DependencyManager), which allows to specify "add/change/remove" LogSender aspect callback methods:
DependencyActivatorBase.createAspectService(
Class<?> serviceInterface,
String serviceFilter,
int ranking,
String add,
String change,
String remove);
Then the LogSenderAspect callbacks method signature can take as arguments the Sender service, as well as the Sender service properties Map.
Now, the second (simpler) solution is to specify a service filter for your aspect, and in this case; no need to specify any callbacks.
let's take a look at the first solution with callbacks, where the LogSender aspect defines a "setSender(Sender, Map)" method, and the aspect will then only log the "send" method of Sender services having "foo=bar" service properties (here, we ignore service change/removed callbacks):
public class Activator extends DependencyActivatorBase{
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
Component logSender = createAspectService(Sender.class, null, 10, "setSender", null, null)
.setImplementation(LogSender.class)
.add(createServiceDependency().setService(LogService.class).setRequired(true));
dm.add(logSender);
}
}
class LogSender implements Sender {
volatile Sender sender;
volatile Map<String, Object> senderProperties;
volatile LogService log;
void setSender(Sender sender, Map<String, Object> senderProperties) {
this.sender = sender;
this.senderProperties = senderProperties;
}
#Override
public void send() {
if ("bar".equals(senderProperties.get("foo"))) {
log.log(LogService.LOG_DEBUG, "calling method send called on Sender service having properties foo=bar");
}
this.sender.send();
}
}
Now, a simpler solution consists in using a service filter "(foo=bar)" when defining your aspect, and in this case, no need to use callback:
public class Activator extends DependencyActivatorBase{
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
Component logSender = createAspectService(Sender.class, "(foo=bar)", 10)
.setImplementation(LogSender.class)
.add(createServiceDependency().setService(LogService.class).setRequired(true));
dm.add(logSender);
}
}
class LogSender implements Sender {
volatile Sender sender;
#Override
public void send() {
log.log(LogService.LOG_DEBUG, "calling method send called on Sender service having properties foo=bar");
this.sender.send();
}
}
does this help ?
/Pierre
Related
I am having troubles invoking a method asynchronously in Spring, when the invoker is an embedded library receiving notifications from an external system. The code looks as below:
#Service
public class DefaultNotificationProcessor implements NotificationProcessor {
private NotificationClient client;
#Override
public void process(Notification notification) {
processAsync(notification);
}
#PostConstruct
public void startClient() {
client = new NotificationClient(this, clientPort);
client.start();
}
#PreDestroy
public void stopClient() {
client.stop();
}
#Async
private void processAsync(Notification notification) {
// Heavy processing
}
}
The NotificationClient internally has a thread in which it receives notifications from another system. It accepts a NotificationProcessor in its constructor which is basically the object that will do the actual processing of notifications.
In the above code, I have given the Spring bean as the processor and attempted to process the notification asynchronously by using #Async annotation. However, it appears the notification is processed in the same thread as the one used by NotificationClient. Effectively, #Async is ignored.
What am I missing here?
#Async (as well as #Transactional and other similar annotations) will not work when the method is invoked via this (on when #Async is used for private methods*), as long as you do not use real AspectJ compiletime or runtime weaving.
*the private method thing is: when the method is private, then it must been invoked via this - so this is more the consequence then the cause
So change your code:
#Service
public class DefaultNotificationProcessor implements NotificationProcessor {
#Resource
private DefaultNotificationProcessor selfReference;
#Override
public void process(Notification notification) {
selfReference.processAsync(notification);
}
//the method must not been private
//the method must been invoked via a bean reference
#Async
void processAsync(Notification notification) {
// Heavy processing
}
}
See also the answers for: Does Spring #Transactional attribute work on a private method? -- this is the same problem
In Spring Framework classes that represent an event to be published by the ApplicationEventPublisher and listened to by the #EventListener are
EventObject <- ApplicationEvent <- PayloadApplicationEvent.
My question is what is the non-nullable source in the initial EventObject constructor and all derived subclasses constructors?
Javadocs give a rather vague explanation that it is
"the object upon which the Event in question initially occurred".
Is it an associated domain entity or publisher service or something else?
Additionally, I am confused why is it required at all if #EventListener states that
"Events can be ApplicationEvent instances as well as arbitrary objects"?
I understand source to be the where the event is created. For example, a #Service that processes a web request received from a #Controller. Therefore, when you call ApplicationEventPublisher.publishEvent() the source parameter to the ApplicationEvent event is this, the service.
public class AwesomeEvent extends ApplicationEvent {
private final String howAwesome;
public AwesomeEvent(Object source, String howAwesome) {
super(source);
this.howAwesome = howAwesome;
}
}
#Service
#RequiredArgsConstructor // because, lazy
public class AwesomeService {
private final ApplicationEventPublisher eventPublisher;
public void awesomeMethod() {
// Do superhero awesome stuff
eventPublisher.publishEvent(new AwesomeEvent(this, "Extremely"));
}
}
Just show some code to you. Look at the getApplicationContext() method.
public abstract class ApplicationContextEvent extends ApplicationEvent {
public ApplicationContextEvent(ApplicationContext source) {
super(source);
}
public final ApplicationContext getApplicationContext() {
return (ApplicationContext) getSource();
}
I am trying to add a ErrorHandler via the EventProcessingConfigurer.registerErrorHandler() method and while it is showing on the configuration the class itself is not being called.
Am currently using Axon 4.1.1 (With out Axon server) and Spring Boot 2.1.6.RELEASE.
i have based my code off github/AxonFramework but it isn't acting the same.
Config:
#Autowired
public void configure(final EventProcessingConfigurer config) {
TestErrorHandler testErrorHandler = new TestErrorHandler();
config.registerErrorHandler("SolrProjection", configuration -> testErrorHandler);
}
ErrorHander:
public class TestErrorHandler implements ErrorHandler, ListenerInvocationErrorHandler {
#Override
public void handleError(final ErrorContext errorContext) throws Exception {
System.out.println("TestErrorHandler.handleError()");
}
#Override
public void onError(final Exception exception, final EventMessage<?> event, final EventMessageHandler eventHandler) {
System.out.println("TestErrorHandler.onError()");
}
}
Projection:
#Configuration
#RequiredArgsConstructor
#ProcessingGroup("SolrProjection")
public class SolrProjection {
#EventHandler
public void onEvent(final TestEvent event,
#SequenceNumber Long sequenceNumber,
#Timestamp final Instant requestTimestamp,
#MessageIdentifier final String messageIdentifier,
final MetaData metaData) {
if (true) {
throw new IllegalStateException();
}
}
even thou i am directly throwing an error, i do not ever see the two system.out's in console. and putting log statements in the #EventHandler are properly being called.
The ErrorHandler is tasked to dealing with different exceptions than what you expect.
When it comes to handling events, Axon Framework deduces two layers:
The internal EventProcessor layer
The Event Handling Components written by framework users
Exceptions thrown within the EventProcessor are dealt with by the ErrorHandler you've configured.
For customizing the process for handling exceptions from your own Event Handlers, you
will have to configure the ListenerInvocationErrorHandler.
To configure a general/default ListenerInvocationErrorHandler, you can use the following method in your first snippet:
EventProcessingConfigurer#registerDefaultListenerInvocationErrorHandler(
Function<Configuration, ListenerInvocationErrorHandler>
)
You can also check out Axon's Reference Guide at this page for more info on this.
Hope this helps you out #sherring!
We are working on a multilingual Spring based web application (not Spring Boot).
Now we are searching for the "spring way" of doing the following.
user starts a http session with some parameters, e.g. a locale "de" and/or a country-code "DE" (the type of parameter isn't really important)
user works with application
at some point the user triggers an action that somewhere deep inside needs a "localized" functionality
Example (java pseudocode):
// service with functionality common for all users
#Service
class CommonService implements ICommonService
{
// how to autowire a service based on some info in the actual HttpSession, eg. CustomServiceUK or CustomServiceDE
#Autowired
private ICustomService customServiceImpl;
#Override
public void doSomeAction(String param)
{
... do some common stuff
customResult = customServiceImpl.calculate(param);
... do some common stuff with custom result
}
}
// custom service implementations
#Service("CustomServiceUK")
class CustomServiceUK implements ICustomService
{
#Override
public String calculate(String value)
{
... execute logic on value for an "uk" user
}
}
#Service("CustomServiceDE")
class CustomServiceDE implements ICustomService
{
#Override
public String calculate(String value)
{
... execute logic on value for an "de" user
}
}
How to inject a custom service based on some info in the actual HttpSession (e.g. CustomServiceUK or CustomServiceDE) into CommonService?
What are our options to solve this issue? Is there something like a dynamic #Qualifier or some #Autowired Spring-Factory thing?
(the service implementation to use must not necessarily depend on the locale of the user but on some other piece of session/request information)
Thanks for your answers.
Actually we end up with the following solution which works for us.
We created an additional implementation of ICustomService with name CustomServiceProxy.
This service has #Primary annotation to tell Spring that this component should be injected when no explicit qualifier is supplied.
The service gets the sessionData and a Map with all Spring managed ICustomService-Components injected (Map-Key = Qualifier of the Component).
Now when some method on CustomServiceProxy gets called, it generates the Map-Key based on the actual sessionData (e.g. language), lookup the ICustomService in the Map and delegates the call to this specific service.
// service with functionality common for all users
#Service
class CommonService implements ICommonService
{
// because of #Primary an instance of CustomServiceProxy will be injected
#Autowired
private ICustomService customServiceImpl;
#Override
public void doSomeAction(String param)
{
... do some common stuff
customResult = customServiceImpl.calculate(param);
... do some common stuff with custom result
}
}
// custom service implementations
#Service
#Primary
class CustomServiceProxy implements ICustomService
{
private CustomData sessionData;
private Map<String, ICustomService> services;
#Autowired
public CustomServiceProxy(CustomData sessionData, Map<String, ICustomService> services)
{
this.sessionData = sessionData;
this.services = services;
}
#Override
public String calculate(String value)
{
String serviceName = "CustomService" + sessionData.getLanguage().toUpperCase();
ICustomService customService = services.get(serviceName);
// handle missing service: throw exception or maybe switch to a default implementation
Objects.requireNonNull(customService, "missing CustomService with name " + serviceName);
return customService.calculate(value);
}
}
#Service("CustomServiceUK")
class CustomServiceUK implements ICustomService
{
#Override
public String calculate(String value)
{
... execute logic on value for an "uk" user
}
}
#Service("CustomServiceDE")
class CustomServiceDE implements ICustomService
{
#Override
public String calculate(String value)
{
... execute logic on value for an "de" user
}
}
Is it possible to use callbacks with Spring to that they are managed by application context?
My problem is when a service is used from outer by #Autowired, but within that service there is a callback defined using new operator.
The following example executes a method that is worth retrying. Spring offers a RetryCallback for this case (I know this could be acchieved differently, but just to illustrate my callback problem).
#Service
class MyService {
//main method invoked
void run(DataVO dataVO) {
//new operator not usable in spring context
RetryCallback<Object> retryCallback = new RetryCallback<Object>() {
#Override
public Object doWithRetry(RetryContext context) throws Exception {
return createBooking(dataVO);
}
};
}
private Object createBooking(DataVO dataVO) {
//creates the booking, worth retry on specific failures
//uses further injected/autowired services here
}
}
Is it possible to refactor this snippet so that the callback is managed by spring/injected/autowired?
Make your service implement the callback interface :
#Service
class MyService implements RetryCallback<Object> {
//main method invoked
void run(DataVO dataVO) {
}
#Override
public Object doWithRetry(RetryContext context) throws Exception {
return createBooking(dataVO);
}
private Object createBooking(DataVO dataVO) {
//creates the booking, worth retry on specific failures
//uses further injected/autowired services here
}
}