Skip to content

Commit

Permalink
Gestion de la reconnexion a rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann151924 committed Dec 13, 2018
1 parent 736f019 commit 83e210f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,7 @@ public void onDisable() {
}

public ChatService getChatService(){
ChatService chatService = null;
try {
chatService = new ChatService(config.getUri(), this);
}
catch (Exception e){
sendConsoleMessage("&8[&eAurionChat&8]&e - Connection error with the rabbitmq instance");
e.printStackTrace();
Bukkit.getPluginManager().disablePlugin(this);
}
return chatService;
return new ChatService(config.getUri(), this);
}

public Config getConfigPlugin(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ChatService extends ChatServiceCommun {
private Config config;
private Utils utils;

public ChatService(String uri, AurionChat plugin) throws Exception {
public ChatService(String uri, AurionChat plugin){
super(uri);
this.plugin = plugin;
this.config = plugin.getConfigPlugin();
Expand Down Expand Up @@ -46,4 +46,9 @@ public void sendMessage(String channelName, String message){
public String getCHANNEL(){
return AurionChat.CHANNEL;
}

@Override
public void desactivatePlugin(){
Bukkit.getPluginManager().disablePlugin(plugin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ public abstract class ChatServiceCommun {
private List<String> messageBuffer;
private String CHANNEL;

public ChatServiceCommun(String uri) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException, IOException, TimeoutException {
public ChatServiceCommun(String uri){
ConnectionFactory factory = new ConnectionFactory();
factory.setUri(uri);
try{
factory.setUri(uri);
}
catch (KeyManagementException|URISyntaxException|NoSuchAlgorithmException UriKeyException){
desactivatePlugin();
System.out.println("Uri Syntax Exception, please check the config or the documentation of rabbitmq");
}
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(5000);
this.connection = factory.newConnection();
this.channel = this.connection.createChannel();
this.messageBuffer = new ArrayList<>();
this.CHANNEL = getCHANNEL();
try{
connection = factory.newConnection();
channel = connection.createChannel();
}
catch (IOException|TimeoutException exception){
System.out.println("Connection error with rabbitmq");
System.out.println(exception.getMessage());
}
messageBuffer = new ArrayList<>();
CHANNEL = getCHANNEL();
}

public void join(String serverName) throws IOException{
Expand Down Expand Up @@ -84,6 +96,7 @@ private String getChannelName(String message){
}

abstract public void sendMessage(String channelName, String message);
abstract public void desactivatePlugin();
abstract public String getCHANNEL();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingEvent;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.scheduler.Task;
Expand Down Expand Up @@ -85,8 +86,13 @@ public void Init(GamePreInitializationEvent event) throws IOException, ObjectMap
}

@Listener
public void onServerStart(GameStartedServerEvent event) throws IOException {
getLogger().info("Successfully running AurionChat - Debug");
public void onServerStart(GameStartedServerEvent event){
getLogger().info("Successfully running AurionChat");
}

@Listener
public void onServerStop(GameStoppingEvent event) throws IOException{
getChatService().leave(config.rabbitmq.servername);
}


Expand All @@ -111,19 +117,19 @@ public void loadCommands(AurionChat plugin){
}

public ChatService getChatService(){
ChatService chatService = null;
// ChatService chatService = null;
config = getConfig();
try{
chatService = new ChatService(config.rabbitmq.uri, this);
}
catch(Exception e){
getLogger().error("Connection error with the rabbitmq instance");
e.printStackTrace();
Sponge.getEventManager().unregisterListeners(this);
Sponge.getCommandManager().getOwnedBy(this).forEach(Sponge.getCommandManager()::removeMapping);
Sponge.getScheduler().getScheduledTasks(this).forEach(Task::cancel);
}
return chatService;
//try{
// chatService = new ChatService(config.rabbitmq.uri, this);
// }
// catch(Exception e){
// getLogger().error("Connection error with the rabbitmq instance");
// e.printStackTrace();
// Sponge.getEventManager().unregisterListeners(this);
// Sponge.getCommandManager().getOwnedBy(this).forEach(Sponge.getCommandManager()::removeMapping);
// Sponge.getScheduler().getScheduledTasks(this).forEach(Task::cancel);
// }
return new ChatService(config.rabbitmq.uri, this);
}

public void sendConsoleMessage(String message){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
import com.mineaurion.aurionchat.sponge.AurionChat;
import com.mineaurion.aurionchat.sponge.Config;
import com.mineaurion.aurionchat.sponge.Utils;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.scheduler.Task;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.concurrent.TimeoutException;

public class ChatService extends ChatServiceCommun {
private AurionChat plugin;
private Config config;
private Utils utils;

public ChatService(String uri, AurionChat plugin) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException, IOException, TimeoutException {
public ChatService(String uri, AurionChat plugin){
super(uri);
this.plugin = plugin;
this.config = plugin.getConfig();
Expand All @@ -30,7 +27,7 @@ public void sendMessage(String channelName, String message){
String messageClean = message.replace(channelName + " ", "");
//#TODO a check
if(config.options.automessage){
Set<String> automessageChannels = config.channels.keySet();
Set<String> automessageChannels = config.automessage.keySet();
if(automessageChannels.contains(channel)){
utils.broadcastToPlayer(channel, message);
}
Expand All @@ -45,4 +42,11 @@ public void sendMessage(String channelName, String message){
public String getCHANNEL(){
return AurionChat.CHANNEL;
}

@Override
public void desactivatePlugin(){
Sponge.getEventManager().unregisterListeners(plugin);
Sponge.getCommandManager().getOwnedBy(plugin).forEach(Sponge.getCommandManager()::removeMapping);
Sponge.getScheduler().getScheduledTasks(plugin).forEach(Task::cancel);
}
}

0 comments on commit 83e210f

Please sign in to comment.