IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Java Discussion :

[Jedis] Problème de requette avec Jedis


Sujet :

API standards et tierces Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2017
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2017
    Messages : 7
    Points : 6
    Points
    6
    Par défaut [Jedis] Problème de requette avec Jedis
    Bonjours, je poste ce topic car je rencontre quelque problème avec mon plugin minecraft qui utilise Jedis pour communiquer avec redis server, en faite j'ai cette erreur qui sort de ma console

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.Long
    	at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265)
    	at redis.clients.jedis.Jedis.del(Jedis.java:197)
    	at mc.bloostrynetwork.antox11200.bloostryapi.utils.database.redis.RedisClient$4.run(RedisClient.java:175)
    	at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
    	at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    	... 3 more
    Ligne ciblée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    getJedis().del(PATH_BLOOSTRYPLAYER+uuid.toString());
    Code entier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
     
    package mc.bloostrynetwork.antox11200.bloostryapi.utils.database.redis;
     
    import java.util.Set;
    import java.util.UUID;
     
    import org.bukkit.Bukkit;
     
    import com.google.gson.Gson;
     
    import mc.bloostrynetwork.antox11200.bloostryapi.Main;
    import mc.bloostrynetwork.antox11200.bloostryapi.utils.IntegerUtils;
    import mc.bloostrynetwork.antox11200.bloostryapi.utils.console.Console;
    import mc.bloostrynetwork.antox11200.bloostryapi.utils.database.RequestDatabase;
    import mc.bloostrynetwork.antox11200.bloostryapi.utils.player.BloostryPlayer;
    import mc.bloostrynetwork.antox11200.bloostryapi.utils.server.ServerStatus;
    import net.minecraft.server.v1_8_R3.ExceptionUnknownCommand;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
     
    public class RedisClient extends RequestDatabase {
     
    	public final String PATH_BLOOSTRYPLAYER = "bloostryPlayers:";
    	public final String PATH_SERVER_STATUS = "serverStatus:status:";
    	public final String PATH_SERVER_ONLINE = "serverStatus:playersonline:";
     
    	public static final String PREFIX = "§8[§cRedis§8] ";
     
    	private String name;
    	private String adress;
    	private String password;
     
    	private int port;
     
    	public JedisPool jedisPool;
     
    	public RedisClient(String name, String adress, int port, String password) {
    		this.name = name;
    		this.adress = adress;
    		this.port = port;
    		this.password = password;
    	}
     
    	public void connection() {
    	    ClassLoader previous = Thread.currentThread().getContextClassLoader();
    	    Thread.currentThread().setContextClassLoader(RedisClient.class.getClassLoader());
    	    jedisPool = new JedisPool(this.adress, this.port);
    	    Thread.currentThread().setContextClassLoader(previous);
    		Console.sendMessageInfo(PREFIX + "§aRedis client is connected from server redis :D");
    	}
     
    	public void deconnection() {
    		this.jedisPool.close();
    		Console.sendMessageInfo(PREFIX + "§aRedis client is disconnected from server redis \nGood Bye :D");
    	}
     
    	public boolean isConnected() {
    		if(jedisPool == null)
    			return false;
    		if(getJedis() == null)
    			return false;
    		if(getJedis().isConnected() && (!(jedisPool.isClosed()))) 
    			return true;
    		return false;
    	}
     
    	/*public JedisCluster getJedisCluster() {
    		return this.jedisCluster;
    	}*/
     
    	public Jedis getJedis() {
    		try (Jedis jedis = jedisPool.getResource()){
    			jedis.auth(this.password);
    			return jedis;
    		} catch (Exception e){
    			Console.sendMessageError("§cJedis can't recover variable !");
    			return null;
    		}
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public String getAdress() {
    		return adress;
    	}
     
    	public String getPassword() {
    		return password;
    	}
     
    	public int getPort() {
    		return port;
    	}
     
    	// TOUTES LES METHODES CI-DESSOUS SONT DES METHODES EXTENDU DE LA CLASSE
    	// 'RequestDatabase'
     
    	@Override
    	public boolean hasUser(String name) {
    		return Main.bloostryAPI.getDatabaseManager().getMySQLClient().hasUser(name);
    	}
     
    	@Override
    	public boolean hasData(UUID uuid) {
    		return getJedis().get(PATH_BLOOSTRYPLAYER + uuid.toString()) != null;
    	}
     
    	@Override
    	public String getUUID(String name) {
    		return Main.bloostryAPI.getDatabaseManager().getMySQLClient().getUUID(name);
    	}
     
    	@Override
    	public BloostryPlayer getData(UUID uuid) {
    		if (!(hasData(uuid)))
    			return null;
    		String bloostryPlayerJson = getJedis().get(PATH_BLOOSTRYPLAYER + uuid.toString());
    		String firstLetter = bloostryPlayerJson.substring(0, 1);
    		String lastLetter = bloostryPlayerJson.substring(bloostryPlayerJson.length() - 1, bloostryPlayerJson.length());
    		if (firstLetter.equalsIgnoreCase("'") && lastLetter.equalsIgnoreCase("'")) {
    			bloostryPlayerJson = bloostryPlayerJson.substring(1, bloostryPlayerJson.length() - 1);
    		} else if (firstLetter.equalsIgnoreCase("'") && !lastLetter.equalsIgnoreCase("'")) {
    			bloostryPlayerJson = bloostryPlayerJson.substring(1, bloostryPlayerJson.length());
    		} else if (!firstLetter.equalsIgnoreCase("'") && lastLetter.equalsIgnoreCase("'")) {
    			bloostryPlayerJson = bloostryPlayerJson.substring(0, bloostryPlayerJson.length() - 1);
    		}
    		bloostryPlayerJson = bloostryPlayerJson.replace("\\", "");
    		Console.sendMessageDebug(bloostryPlayerJson);
    		return new Gson().fromJson(bloostryPlayerJson, BloostryPlayer.class);
    	}
     
    	@Override
    	public void createUser(String name, UUID uuid) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				Main.bloostryAPI.getDatabaseManager().getMySQLClient().createUser(name, uuid);
    			}
    		});
    	}
     
    	@Override
    	public void createData(UUID uuid, BloostryPlayer bloostryPlayer) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				String bloostryPlayerJson = new Gson().toJson(bloostryPlayer);
    				if (!(hasData(uuid))) {
    					setData(uuid, bloostryPlayer);
    				} else {
    					Console.sendMessageDebug(
    							PREFIX + "§cThis data is not created (" + uuid + "," + bloostryPlayerJson + ")");
    				}
    			}
    		});
    	}
     
    	@Override
    	public void deleteUser(String name) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				Main.bloostryAPI.getDatabaseManager().getMySQLClient().deleteUser(name);
    			}
    		});
    	}
     
    	@Override
    	public void deleteData(UUID uuid) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				if (hasData(uuid)) {
    					getJedis().del(PATH_BLOOSTRYPLAYER + uuid.toString());
    				} else {
    					Console.sendMessageDebug(PREFIX + "§cThis data is not created (" + uuid + ")");
    				}
    			}	
    		});
    	}
     
    	@Override
    	public void setData(UUID uuid, BloostryPlayer bloostryPlayer) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				String bloostryPlayerJson = new Gson().toJson(bloostryPlayer);
    				getJedis().set(PATH_BLOOSTRYPLAYER + uuid.toString(), "'" + bloostryPlayerJson + "'");
    				Console.sendMessageDebug("SET " + PATH_BLOOSTRYPLAYER + uuid.toString() + "'" + bloostryPlayerJson + "'");
    			}
    		});
    	}
     
    	public void addPlayerOnline(String serverName) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    			}
    		});
    		Integer playersOnline = getPlayersOnline(serverName);
    		getJedis().set(PATH_SERVER_ONLINE + serverName, ((playersOnline == null ? 0 : playersOnline) + 1) + "");
    	}
     
    	public void removePlayerOnline(String serverName) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				Integer playersOnline = getPlayersOnline(serverName);
    				getJedis().set(PATH_SERVER_ONLINE + serverName, ((playersOnline == null ? 0 : playersOnline) - 1) + "");
    			}
    		});
    	}
     
    	public Integer getPlayersOnline(String serverName) {
    		String reponse = getJedis().get(PATH_SERVER_ONLINE + serverName);
    		if (reponse == null)
    			return null;
    		if (!IntegerUtils.isInteger(reponse))
    			return 0;
    		return IntegerUtils.getValueOf(reponse);
    	}
     
    	public Integer getPlayersOnlineOnNetwork() {
    		Set<String> servers = Main.bloostryAPI.getDatabaseManager().getJedis().smembers("serversList");
    		Integer onlines = 0;
    		for (String serverName : servers) {
    			onlines += (getPlayersOnline(serverName) == null ? 0 : getPlayersOnline(serverName));
    		}
    		return onlines;
    	}
     
    	public void setServerStatus(String serverName, ServerStatus serverStatus) {
    		Bukkit.getScheduler().runTaskAsynchronously(Main.bloostryAPI.getJavaPlugin(), new Runnable() {
    			@Override
    			public void run() {
    				getJedis().set(PATH_SERVER_STATUS + serverName, serverStatus.getKeyPath());
    			}
    		});
    	}
     
    	public ServerStatus getServerStatus(String serverName) {
    		String status = getJedis().get(PATH_SERVER_STATUS + serverName);
    		if (status == null)
    			return null;
    		return ServerStatus.getServerStatusType(status);
    	}
    }

  2. #2
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Hello,


    regarde ta méthode getJedis().

    Elle récupère une instance de Jedis dans le try-with-resource, puis elle quitte immédiatement le try-with-resource en faisant un return de cette instance, qui se retrouve donc fermée.
    Si elle est fermée, elle n'est pas dans un état utilisable, et il ne faut donc pas s'en servir.

    Tu ne peux pas faire un return dans le try-with-resource. Il faut que tu fasses tout le travail que tu as à faire avec cette instance de Jedis, et une fois seulement que tu as fini de t'en servir, tu peux quitter le try-with-resource. Pas avant.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2017
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2017
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    et comment faire ceci ?

  4. #4
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Que te disent les exemples d'utilisation de Jedis ?

    Je suppose que ce try-with-resource ne vient pas de nulle part... Et pourtant les exemples que tu as vus ne font pas de return à l'intérieur, eux.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2017
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2017
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    Bon, je suis désolé mais je ne comprends rien à ce que tu as dit je ne vois pas ce que je dois modifier

  6. #6
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Tu as un return dans le try-with-resources. Tu dois changer beaucoup de choses, pour que tu n'aies plus de return dans le try-with-resource.

    Au départ tu n'aurais jamais dû avoir un code comme ça. Alors efface et recommence, sans avoir de return dans le try-with-resource.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  7. #7
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2017
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2017
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    Sauf que le problème je ne sais pas je dois changer quoi pour faire un nouveau code :/

  8. #8
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Je dirais pour commencer, pas de méthode getJedis().

    Tu fais un try-with-resource pour obtenir une instance de Jedis, et tu t'en sers à l'intérieur. Pas de méthode qui sert à obtenir une instance, autre que l'appel à la pool lui-même.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. [PDO] Problème de requette avec pdo
    Par g_escande81 dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 21/05/2010, 09h53
  2. Problème requette avec valeur NULL
    Par Destiny dans le forum Langage SQL
    Réponses: 3
    Dernier message: 21/05/2007, 08h35
  3. Problème dans requête avec count()
    Par BadFox dans le forum Requêtes
    Réponses: 3
    Dernier message: 08/07/2003, 18h02
  4. [Kylix] problème compilation kylix3 avec redhat 9.0
    Par madininaoursa dans le forum EDI
    Réponses: 1
    Dernier message: 02/07/2003, 16h21
  5. Problèmes de versions avec Xalan, Xerces et Java
    Par honeyz dans le forum XML/XSL et SOAP
    Réponses: 4
    Dernier message: 05/06/2003, 10h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo