Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send application events before/after RabbitMQ consumer subscribe to a queue #515

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.rabbitmq.docs.event

import io.micronaut.context.annotation.Requires
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener
import io.micronaut.rabbitmq.event.RabbitConsumerStarted
import jakarta.inject.Singleton
import org.slf4j.Logger
import org.slf4j.LoggerFactory
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
class MyStartedEventListener implements ApplicationEventListener<RabbitConsumerStarted> {
private static final Logger LOG = LoggerFactory.getLogger(MyStartingEventListener.class)
@Override
void onApplicationEvent(RabbitConsumerStarted event) {
LOG.info("RabbitMQ consumer: {} (method: {}) just subscribed to: {}",
event.source, event.method, event.queue);
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.rabbitmq.docs.event

import io.micronaut.context.annotation.Requires
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener
import io.micronaut.rabbitmq.event.RabbitConsumerStarting
import jakarta.inject.Singleton
import org.slf4j.Logger
import org.slf4j.LoggerFactory
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
class MyStartingEventListener implements ApplicationEventListener<RabbitConsumerStarting> {
private static final Logger LOG = LoggerFactory.getLogger(MyStartingEventListener.class)
@Override
void onApplicationEvent(RabbitConsumerStarting event) {
LOG.info("RabbitMQ consumer: {} (method: {}) is subscribing to: {}",
event.source, event.method, event.queue);
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.rabbitmq.docs.event;

import io.micronaut.context.annotation.Requires;
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.rabbitmq.event.RabbitConsumerStarted;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
public class MyStartedEventListener implements ApplicationEventListener<RabbitConsumerStarted> {
private static final Logger LOG = LoggerFactory.getLogger(MyStartingEventListener.class);
@Override
public void onApplicationEvent(RabbitConsumerStarted event) {
LOG.info("RabbitMQ consumer: {} (method: {}) just subscribed to: {}",
event.getSource(), event.getMethod(), event.getQueue());
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.rabbitmq.docs.event;

import io.micronaut.context.annotation.Requires;
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.rabbitmq.event.RabbitConsumerStarting;
import jakarta.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
public class MyStartingEventListener implements ApplicationEventListener<RabbitConsumerStarting> {
private static final Logger LOG = LoggerFactory.getLogger(MyStartingEventListener.class);
@Override
public void onApplicationEvent(RabbitConsumerStarting event) {
LOG.info("RabbitMQ consumer: {} (method: {}) is subscribing to: {}",
event.getSource(), event.getMethod(), event.getQueue());
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.micronaut.rabbitmq.docs.event

import io.micronaut.context.annotation.Requires
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener
import io.micronaut.rabbitmq.event.RabbitConsumerStarted
import jakarta.inject.Singleton
import org.slf4j.LoggerFactory
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
class MyStartedEventListener : ApplicationEventListener<RabbitConsumerStarted> {
private val LOG = LoggerFactory.getLogger(javaClass)
override fun onApplicationEvent(event: RabbitConsumerStarted) {
LOG.info("RabbitMQ consumer: {} (method: {}) just subscribed to: {}",
event.source, event.method, event.queue)
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.micronaut.rabbitmq.docs.event

import io.micronaut.context.annotation.Requires
// tag::imports[]
import io.micronaut.context.event.ApplicationEventListener
import io.micronaut.rabbitmq.event.RabbitConsumerStarting
import jakarta.inject.Singleton
import org.slf4j.LoggerFactory
// end::imports[]

@Requires(property = "spec.name", value = "RabbitListenerEventsSpec")
// tag::clazz[]
@Singleton
class MyStartingEventListener : ApplicationEventListener<RabbitConsumerStarting> {
private val LOG = LoggerFactory.getLogger(javaClass)
override fun onApplicationEvent(event: RabbitConsumerStarting) {
LOG.info("RabbitMQ consumer: {} (method: {}) is subscribing to: {}",
event.source, event.method, event.queue)
}
}
// end::clazz[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.rabbitmq.event;

/**
* Abstract class for RabbitMQ consumer events.
*
* @since 4.1.0
*/
public abstract class AbstractRabbitConsumerEvent extends AbstractRabbitEvent<Object> {

private final String method;
private final String queue;

/**
* Default constructor.
*
* @param bean The bean annotated as {@link io.micronaut.rabbitmq.annotation.RabbitListener}
* @param method The consumer method
* @param queue The name of the queue the consumer subscribes to
*/
protected AbstractRabbitConsumerEvent(Object bean, String method, String queue) {
super(bean);
this.method = method;
this.queue = queue;
}

/**
* @return The name of the queue the consumer subscribes to.
*/
public String getQueue() {
return queue;
}

/**
* @return The consumer method.
*/
public String getMethod() {
return method;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.rabbitmq.event;

import io.micronaut.context.event.ApplicationEvent;

/**
* Abstract class for RabbitMQ events.
*
* @param <T> The source type
* @since 4.1.0
*/
public abstract class AbstractRabbitEvent<T> extends ApplicationEvent {

/**
* Default constructor.
*
* @param source The source
*/
protected AbstractRabbitEvent(T source) {
super(source);
}

@Override
public T getSource() {
return (T) super.getSource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.rabbitmq.event;

/**
* An event fired after a {@link io.micronaut.rabbitmq.annotation.RabbitListener} subscribes to a queue.
*
* @since 4.1.0
*/
public class RabbitConsumerStarted extends AbstractRabbitConsumerEvent {

/**
* Default constructor.
*
* @param bean The bean annotated as {@link io.micronaut.rabbitmq.annotation.RabbitListener}
* @param method The consumer method
* @param queue The name of the queue the consumer subscribes to
*/
public RabbitConsumerStarted(Object bean, String method, String queue) {
super(bean, method, queue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017-2020 original 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 io.micronaut.rabbitmq.event;

/**
* An event fired before a RabbitMQ consumer subscribes to a queue.
*
* @since 4.1.0
*/
public class RabbitConsumerStarting extends AbstractRabbitConsumerEvent {

/**
* Default constructor.
*
* @param bean The bean annotated as {@link io.micronaut.rabbitmq.annotation.RabbitListener}
* @param method The consumer method
* @param queue The name of the queue the consumer subscribes to
*/
public RabbitConsumerStarting(Object bean, String method, String queue) {
super(bean, method, queue);
}
}
Loading