import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.text.NumberFormat; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; /** * Class that simulates the bug * * */ public class AgentLoader { static AgentLoader agentLoader; public static void main (String args[]) { for(int i = 0 ;i <1 ; i++) { try{ new Thread(new AgentExample("Agent"+i)).start(); // new AgentExample("Agent"+i).run(); Thread.sleep(50); }catch (Exception e) { e.printStackTrace(); } } /*Thread t1 = new Thread(new AgentExample("Agent1")); Thread t2 = new Thread(new AgentExample("Agent2")); Thread t3 = new Thread(new AgentExample("Agent3")); Thread t4 = new Thread(new AgentExample("Agent4")); Thread t5 = new Thread(new AgentExample("Agent5")); Thread t6 = new Thread(new AgentExample("Agent6")); Thread t7 = new Thread(new AgentExample("Agent7")); try{ t1.start(); Thread.sleep(500); t2.start(); Thread.sleep(500); t3.start(); Thread.sleep(500); t4.start(); Thread.sleep(500); t5.start(); Thread.sleep(500); t6.start(); Thread.sleep(500); t7.start(); Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } } class AgentExample extends Thread { private String ip = "localhost"; private String id= null; public AgentExample(String id) { this.id = id; } int inc = 1; OutputStreamWriter wr; public void run() { DataInputStream in = null; try { URL url = new URL("http://"+ip+":8080/test/mycomet"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(0); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); con.connect(); // Initiate the connection with client info String data = URLEncoder.encode("client", "UTF-8") + "=" + URLEncoder.encode(id, "UTF-8"); wr = new OutputStreamWriter(con.getOutputStream()); System.out.println("AGENT Reg DATA:" + data); wr.write(data); wr.flush(); // wr.close(); int i = 0; // Get the response System.out.println("Setting up BufferReader"); BufferedReader rd = new BufferedReader(new InputStreamReader( con.getInputStream())); System.out.println("Getting data"); String message; boolean isRun = true; do { if ((message = rd.readLine()) != null) { System.out.println(" Comet Client " + i++ + " -> GOT " + id + " Message: " + message); sendData(); long timeout = (long)(Math.random()*3000); System.out.println("timeout:"+ timeout); Thread.sleep(timeout); } else { //rd.close(); isRun = false; } //Thread.sleep(10); //System.out.println((long)(Math.random()*1000)); } while (isRun); System.out.println("die!!!"); con.disconnect(); // rd.close(); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } } public void sendData() throws Exception { HttpClient client = new HttpClient(); HttpMethod method = null; System.out.println("Sending back data"); method = new GetMethod("http://"+ip+":8080/comet/mycomet"); method.addRequestHeader("client", id); String msg = "MSG ID" + inc++ + " from Agent id:" + id; System.out.println("Sending back msg -> "+msg); method.setRequestHeader("result",msg); try { client.executeMethod(method); method.releaseConnection(); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }