Skip to content

Commit

Permalink
🔧 chore: update rmq4j service #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Aug 17, 2024
1 parent ef02526 commit 48a1f7e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 6 deletions.
99 changes: 99 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/config/Rmq4jBeanConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.rmq4j.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
* Configures access to the Spring application context.
*/
@Component
public class Rmq4jBeanConfig implements BeanFactoryPostProcessor {

private static ConfigurableListableBeanFactory beanFactory;

/**
* Retrieves the ConfigurableListableBeanFactory instance.
*
* @return the ConfigurableListableBeanFactory instance
*/
public static ConfigurableListableBeanFactory getBeanFactory() {
return beanFactory;
}

/**
* Retrieves a bean by name from the Spring application context.
*
* @param name the name of the bean to retrieve
* @param <T> the type of the bean
* @return the bean instance
* @throws BeansException if an error occurs while retrieving the bean
*/
@SuppressWarnings({"unchecked"})
public static <T> T getBean(String name) throws BeansException {
return (T) getBeanFactory().getBean(name);
}

/**
* Retrieves a bean by type from the Spring application context.
*
* @param clazz the class of the bean to retrieve
* @param <T> the type of the bean
* @return the bean instance
* @throws BeansException if an error occurs while retrieving the bean
*/
public static <T> T getBean(Class<T> clazz) throws BeansException {
return getBeanFactory().getBean(clazz);
}

/**
* Checks if a bean with the specified name exists in the Spring application context.
*
* @param name the name of the bean to check
* @return true if the bean exists, false otherwise
*/
public static boolean containsBean(String name) {
return getBeanFactory().containsBean(name);
}

/**
* Checks if the bean with the specified name is a singleton in the Spring application context.
*
* @param name the name of the bean to check
* @return true if the bean is a singleton, false otherwise
* @throws NoSuchBeanDefinitionException if the bean definition cannot be found
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().isSingleton(name);
}

/**
* Retrieves the type of the bean with the specified name from the Spring application context.
*
* @param name the name of the bean
* @return the type of the bean
* @throws NoSuchBeanDefinitionException if the bean definition cannot be found
*/
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getType(name);
}

/**
* Retrieves the aliases for the bean with the specified name from the Spring application context.
*
* @param name the name of the bean
* @return an array of alias names
* @throws NoSuchBeanDefinitionException if the bean definition cannot be found
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getAliases(name);
}

@SuppressWarnings({"NullableProblems"})
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
Rmq4jBeanConfig.beanFactory = factory;
}
}
4 changes: 4 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.Map;
import java.util.Optional;

public interface Rmq4jService {

@SuppressWarnings({"BooleanMethodIsAlwaysInverted"})
boolean isEnabled();

boolean isDebugging();

Map<String, Rmq4jProperties.Node> getClusters();

Optional<Rmq4jProperties.Node> getNode(String key);

Optional<Rmq4jProperties.Node> getNodeActivated(String key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ public class Rmq4jInsServiceImpl implements Rmq4jInsService {
protected final Map<String, CachingConnectionFactory> factories = new ConcurrentHashMap<>();
protected final Map<String, RabbitTemplate> templates = new ConcurrentHashMap<>();

protected final Rmq4jProperties properties;
protected final Rmq4jService rmq4jService;

@Autowired
public Rmq4jInsServiceImpl(Rmq4jProperties properties,
Rmq4jService rmq4jService) {
this.properties = properties;
public Rmq4jInsServiceImpl(
Rmq4jService rmq4jService) {
this.rmq4jService = rmq4jService;
}

Expand All @@ -50,7 +48,7 @@ public void snapIns() {
if (this.exists()) {
return;
}
for (Map.Entry<String, Rmq4jProperties.Node> entry : properties.getClusters().entrySet()) {
for (Map.Entry<String, Rmq4jProperties.Node> entry : rmq4jService.getClusters().entrySet()) {
if (!entry.getValue().isEnabled()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public boolean isDebugging() {
return properties.isDebugging();
}

/**
* @return map of cluster configuration, class {@link Map}
*/
@Override
public Map<String, Rmq4jProperties.Node> getClusters() {
return properties.getClusters();
}

/**
* Retrieves a specific cluster configuration based on the provided key.
* <p>
Expand All @@ -66,7 +74,7 @@ public Optional<Rmq4jProperties.Node> getNode(String key) {
if (!this.isEnabled() || String4j.isEmpty(key)) {
return Optional.empty();
}
return properties.getClusters().entrySet().stream().filter(e -> e.getKey().equals(key)).map(Map.Entry::getValue).findFirst();
return this.getClusters().entrySet().stream().filter(e -> e.getKey().equals(key)).map(Map.Entry::getValue).findFirst();
}

/**
Expand Down

0 comments on commit 48a1f7e

Please sign in to comment.