Reputation: 3759
My objective is to listen for particular tcp connection events and perform some tasks with it.
By implementing ApplicationEventPublisher
, I can see the events firing.
@Component
public class TestEventPublisher implements ApplicationEventPublisher {
@Override
public void publishEvent(Object event) {
LOG.info("Event Fired - {}", event.getClass());
}
}
INFO [globalTaskExecutor-2] logger - Event Fired - class org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent
INFO [ThreadPoolTaskExecutor-3] logger - Event Fired - class org.springframework.integration.ip.tcp.connection.TcpConnectionCloseEvent
So I added the listeners for it, but they do not get executed.
@Component
public class TestListeners {
@EventListener(TcpConnectionOpenEvent.class)
public void doSomethingWhenConnectionIsOpen() {
LOG.info("----------------**************** CONNECTION OPEN EVENT FIRED ****************** -----------------------"); // No log shows up
}
@EventListener(TcpConnectionFailedEvent.class)
public void doSomethingElseWhenConnectionFails() {
LOG.info("----------------**************** CONNECTION ATTEMPT FAILED EVENT FIRED ****************** -----------------------"); // No Log shows up
}
}
What i am doing wrong?
Upvotes: 1
Views: 1258
Reputation: 174484
Your publisher doesn't actually publish anything, it just logs the event.
In order to use @EventListener
, you must use the built in ApplicationEventPublisher
provided by Spring (which is actually the application context).
The TCP components implement ApplicationEventPublisherAware
so Spring will automatically inject the built-in publisher (as long as the component is managed by Spring). If you want to intercept the publishing, your publisher must implement ApplicationEventPublisherAware
, log the event, then call the real publisher provided by Spring.
You should also add the event parameter to the listeners.
Upvotes: 2