I have an abstract class in a library project where I do:
this.eventGateway.publishEvent("SomeEvent", null);
and EventGateway is this:
@MessagingGateway
public interface EventGateway {
@Gateway(requestChannel = "SomeChannel")
void publishEvent(@Header(value = "EventName") String event, @Payload Object payload);
}
and my SomeChannel definition:
@Bean(name = "SomeChannel)
public MessageChannel someChannel() {
return new PublishSubscribeChannel(Executors.newCachedThreadPool());
}
Now it's been working fine until we wanted to use the library in a multi-tenant environment, where we want to add a header (say, "TenantId") to every message we send using EventGateway. The obvious solution would be adding EventGateway this method:
@Gateway(requestChannel = "SomeChannel")
void publishTenantEvent(@Header(value = "EventName") String event, @Header(value = "TenantId") String tenantId, @Payload Object payload);
and using it like:
final TenantContext context = TenantContextHolder.getContext();
final Tenant tenant = context.getTenant();
this.eventGateway.publishEvent("SomeEvent", tenant.getId(), null);
But changing every place where we do this.eventGateway.publishEvent("SomeEvent", null); to above is almost equal to writing the library from scratch.
FWIW, my SomeChannel definition is as follows:
Is there a way that I can add "TenantId" header to every message I send if it's present in TenantContextHolder (a class holding a thread local variable TenantContext)?