Skip to content

Commit

Permalink
♻️ refactor: update codebase #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Aug 16, 2024
1 parent c3952c1 commit 77e6d6c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jInsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.rmq4j.service;

public interface Rmq4jInsService {
}
3 changes: 3 additions & 0 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.rabbitmq.client.ConnectionFactory;
import org.rmq4j.config.props.Rmq4jProperties;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.Optional;
Expand All @@ -23,5 +24,7 @@ public interface Rmq4jService {

Optional<RabbitTemplate> dispatch(Rmq4jProperties.Node node);

Optional<RabbitAdmin> createAdm(Rmq4jProperties.Node node);

String getURLConnSchema(Rmq4jProperties.Node node);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.rmq4j.service.impl;

import org.rmq4j.service.Rmq4jInsService;
import org.rmq4j.service.Rmq4jService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings({"FieldCanBeLocal", "DuplicatedCode"})
@Service
public class Rmq4jInsServiceImpl implements Rmq4jInsService {
protected static final Logger logger = LoggerFactory.getLogger(Rmq4jInsServiceImpl.class);

protected final Rmq4jService rmq4jService;

@Autowired
public Rmq4jInsServiceImpl(Rmq4jService rmq4jService) {
this.rmq4jService = rmq4jService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -175,6 +176,31 @@ public Optional<RabbitTemplate> dispatch(Rmq4jProperties.Node node) {
return Optional.of(new RabbitTemplate(factory.get()));
}

/**
* Creates a {@link RabbitAdmin} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Node)} to create a
* {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
* {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitAdmin}
* and returns it as an {@link Optional}.
* <p>
* If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
* it returns an empty {@link Optional}.
*
* @param node The cluster configuration used to create the {@link CachingConnectionFactory}.
* @return An {@link Optional} containing the {@link RabbitAdmin} if the {@link CachingConnectionFactory}
* was created successfully; otherwise, an empty {@link Optional}.
*/
@SuppressWarnings({"OptionalIsPresent"})
@Override
public Optional<RabbitAdmin> createAdm(Rmq4jProperties.Node node) {
Optional<CachingConnectionFactory> factory = this.createCacheConnFactory(node);
if (!factory.isPresent()) {
return Optional.empty();
}
return Optional.of(new RabbitAdmin(factory.get()));
}

/**
* Generates the connection URL schema for RabbitMQ based on the provided cluster configuration.
* <p>
Expand Down

0 comments on commit 77e6d6c

Please sign in to comment.