Salut a tous
voila j'ai un probleme ,j'ai une application qui fait une utilisation intensive de threads, c'est un bot jabber , voila le code:
Main.java
ConnectionThread.java
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 import connection.ConnectionThread; /** * * @author Kedare */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { String user = "kedare"; String password = "***"; String server = "jabber.fr"; String channels[] = {"testing001@chat.jabberfr.org"}; ConnectionThread chn = new ConnectionThread(server, user, password, channels); chn.start(); chn.join(); } }
ChannelThread.java
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 package connection; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import channel.*; import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.*; /** * * @author Kedare */ public class ConnectionThread extends Thread { String user; String pass; String server; XMPPConnection connection; Logger log; String channels[]; public ConnectionThread(String serverArg, String userArg, String passArg, String channelsArg[]) { this.user = userArg; this.pass = passArg; this.server = serverArg; this.channels = channelsArg; this.log = Logger.getLogger(ConnectionThread.class.getName()); this.log.log(Level.INFO, "ConnectionThread created for " + this.server); } @Override public void run() { try { log.log(Level.INFO, "Connecting"); this.connection = new XMPPConnection(this.server); this.connection.connect(); this.connection.login(this.user, this.pass); this.log.log(Level.INFO, "Preparing to join channels"); for (String channel : channels) { this.log.log(Level.INFO, "Joining " + channel); ChannelThread chan = new ChannelThread(channel,this.connection); chan.start(); chan.join(); } } catch (Exception ex) { this.log.log(Level.SEVERE, null, ex); } } }
ChannelListener.java
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 package channel; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.*; /** * * @author Kedare */ public class ChannelThread extends Thread { XMPPConnection connection; Logger log; String channelName; Chat channel; public ChannelThread(String channelArg, XMPPConnection connArg) { this.channelName = channelArg; this.connection = connArg; this.log = Logger.getLogger(ChannelThread.class.getName()); this.log.log(Level.INFO, "ChannelnThread created for " + this.channelName); } @Override public void run() { try { this.log.log(Level.INFO, "Joining and begin messageListener for " + this.channelName); this.channel = this.connection.getChatManager().createChat(channelName, ChannelListeners.getMessageListener()); this.channel.sendMessage("Hello"); Thread.interrupted(); } catch (Exception ex) { this.log.log(Level.SEVERE, null, ex); } } }
en gros je voudrais que les threads ne s'arrete pas , et continuer a ecouter le listener , mais je n'y arrive pas :/
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package channel; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.packet.Message; /** * * @author Kedare */ public class ChannelListeners { static MessageListener messageListener = new MessageListener() { public void processMessage(Chat chat, Message message) { System.out.println("Receive Message : " + message); } }; public static MessageListener getMessageListener() { return messageListener; } }
savez vous comment faire ? c'est la premiere fois que j'utilise les threads comme ca..
si non , la facon de coder est bonne ?
merci
Partager