Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Xharos committed Jun 2, 2024
2 parents 34a821e + 595a6b6 commit db992bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface ServiceConnection<T> {

void close() throws Exception;

void connect() throws Exception;
void connect();

T getConnection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* File <b>RabbitMQConnection</b> located on fr.islandswars.commons.service.rabbitmq
Expand Down Expand Up @@ -39,50 +38,42 @@
*/
public class RabbitMQConnection implements ServiceConnection<Channel> {

private final AtomicBoolean status;
private ConnectionFactory factory;
private Connection connection;

public RabbitMQConnection() {
this.status = new AtomicBoolean(false);
}
private ConnectionFactory factory;
private Connection connection;

@Override
public void close() throws Exception {
connection.close();
status.set(false);
}

@Override
public void connect() throws IOException, TimeoutException {
public void connect() {
Preconditions.checkNotNull(factory);

this.connection = factory.newConnection();
status.set(true);
try {
this.connection = factory.newConnection();
} catch (IOException | TimeoutException e) {
LogUtils.error(e);
}
}

@Override
public Channel getConnection() {
if (connection == null) {
try {
connect();
return connection.createChannel();
} catch (IOException | TimeoutException e) {
LogUtils.error(e);
}
} else {
try {
return connection.createChannel();
} catch (IOException e) {
LogUtils.error(e);
}
Preconditions.checkNotNull(connection);

try {
return connection.createChannel();
} catch (IOException e) {
LogUtils.error(e);
}
return null;
}

@Override
public boolean isClosed() {
return status.get();
if (connection != null)
return !connection.isOpen();
else return true;
}

@Override
Expand Down

0 comments on commit db992bd

Please sign in to comment.