diff --git a/Crawler/src/JMS.java b/Crawler/src/JMS.java index f5f9f50..07a7642 100644 --- a/Crawler/src/JMS.java +++ b/Crawler/src/JMS.java @@ -41,7 +41,7 @@ private boolean connect(){ TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); topicConn = connFactory.createTopicConnection(user, password); - topicConn.setClientID("peddy"); + //topicConn.setClientID("peddy"); topicConn.start(); topicSession = topicConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); @@ -49,19 +49,9 @@ private boolean connect(){ if(this.producer) topicPublisher = topicSession.createPublisher(topic); else - topicSubscriber=topicSession.createDurableSubscriber(topic, "MySub"); + topicSubscriber=topicSession.createDurableSubscriber(topic, "sub"); + - ctx = new InitialContext(); - topic = (Topic) ctx.lookup("jms/topic/project"); - connFactory = (TopicConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); - topicConn = connFactory.createTopicConnection(user, password); - topicConn.setClientID("peddy"); - topicConn.start(); - topicSession = topicConn.createTopicSession(false,Session.CLIENT_ACKNOWLEDGE); - if(this.producer) - topicPublisher = topicSession.createPublisher(topic); - else - topicSubscriber=topicSession.createDurableSubscriber(topic, "MySub"); }catch(JMSException e){ System.out.println("Server is down! " + e.toString()); @@ -82,6 +72,7 @@ public boolean send(String xmlString){ message = topicSession.createMessage(); message.setStringProperty("xml", xmlString); topicPublisher.publish(message); + topicConn.close(); } catch (JMSException e) { e.printStackTrace(); return false; diff --git a/Crawler/src/NewsCrawler.java b/Crawler/src/NewsCrawler.java index f71b026..1df7197 100644 --- a/Crawler/src/NewsCrawler.java +++ b/Crawler/src/NewsCrawler.java @@ -37,7 +37,7 @@ public class NewsCrawler implements Runnable { public NewsCrawler() { this.pool = new LinkedBlockingQueue(); this.c = new Crawler(); - this.jms = new JMS("topico", "admin", "admin1", true); + this.jms = new JMS("jms/topic/project", "admin", "admin1", true); (new Thread(jms)).start(); } @@ -138,7 +138,7 @@ private boolean validateXML(String xml) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - Schema schema = factory.newSchema(new StreamSource("scheme.xsd")); + Schema schema = factory.newSchema(new StreamSource("/Users/jmcalves275/Desktop/Faculdade/Mestrado/IS/Assignment_1/IS2014/Crawler/scheme.xsd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes()))); @@ -175,7 +175,7 @@ public static void main(String[] args) throws InterruptedException { } if (crawler != null) { - crawler.loadFailed(); + //crawler.loadFailed(); (new Thread(crawler)).start(); } diff --git a/Stats Producer/src/Reader.java b/Stats Producer/src/Reader.java index e7b718f..6a71f35 100644 --- a/Stats Producer/src/Reader.java +++ b/Stats Producer/src/Reader.java @@ -8,16 +8,19 @@ /** * Created by jmcalves275 on 10/10/14. */ -public class Reader extends Thread { - private TopicConnection connection; +public class Reader extends Thread { + private TopicConnection topicConn; boolean check; - TopicSession topicSession; + private TopicSession topicSession; public Reader(TopicConnection topicConn,TopicSession topicSession,boolean check){ - this.connection=topicConn; + this.topicConn=topicConn; this.topicSession=topicSession; this.check=check; } + public void setTopicConn(TopicConnection topicConn){ + this.topicConn=topicConn; + } public void run(){ while(true) { @@ -25,7 +28,8 @@ public void run(){ if (sc.nextLine().compareTo("exit") == 0) { check = true; try { - connection.close(); + topicConn.close(); + System.exit(0); @@ -38,4 +42,6 @@ public void run(){ } } } + + } diff --git a/Stats Producer/src/StatsProducer.java b/Stats Producer/src/StatsProducer.java index a941455..e83703b 100644 --- a/Stats Producer/src/StatsProducer.java +++ b/Stats Producer/src/StatsProducer.java @@ -37,6 +37,7 @@ public class StatsProducer extends Thread { NewsList listaNoticias=new NewsList(); HashMap hashmap = new HashMap(); + Reader keyboardReader; TopicSubscriber topicSubscriber; InitialContext ctx; Topic topic; @@ -47,7 +48,8 @@ public class StatsProducer extends Thread { public StatsProducer() throws NamingException, JMSException { while(!initConection()); - new Reader(topicConn,topicSession,check).start(); + keyboardReader=new Reader(topicConn,topicSession,check); + keyboardReader.start(); System.out.println("You are connected!"); } public boolean initConection(){ @@ -67,18 +69,19 @@ public boolean initConection(){ topicConn.setClientID("admin1"); // create a topic session - topicSession = topicConn.createTopicSession(false,Session.CLIENT_ACKNOWLEDGE); + topicConn.start(); + topicSession = topicConn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); // create a topic subscriber topicSubscriber=topicSession.createDurableSubscriber(topic, "sub"); - topicConn.start(); - }catch(JMSException e){ - System.out.println("Can't establish connection to the server. We'll retry in 5 seconds"); + }catch(JMSException e){ + System.out.println("erro: "+e.toString()); + //System.out.println("Can't establish connection to the server. We'll retry in 5 seconds"); try{ Thread.sleep(5000); }catch(InterruptedException s){ @@ -87,8 +90,8 @@ public boolean initConection(){ return false; }catch(NamingException e){ - - System.out.println("Can't establish connection to the server. We'll retry in 5 seconds"); + System.out.println("erro: "+e.toString()); + //System.out.println("Can't establish connection to the server. We'll retry in 5 seconds"); try{ Thread.sleep(5000); }catch(InterruptedException s){ @@ -105,18 +108,18 @@ public boolean retryConnection() { topicConn.close(); + topicConn = connFactory.createTopicConnection("admin", "admin1"); topicConn.setClientID("admin1"); - // create a topic session + topicConn.start(); TopicSession topicSession = topicConn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); // create a topic subscriber topicSubscriber=topicSession.createDurableSubscriber(topic, "sub"); - topicConn.start(); @@ -146,7 +149,7 @@ public void receive(){ String doc = m.getStringProperty("xml"); System.out.println("Messages received from topic!\n"); - System.out.println(doc); + //System.out.println(doc); checkFicheiro = validateXML("/Users/jmcalves275/Desktop/Faculdade/Mestrado/IS/Assignment_1/IS2014/Stats Producer/scheme.xsd", doc); @@ -169,7 +172,8 @@ public void receive(){ } catch (InterruptedException e1) { e1.printStackTrace(); } - while (!check && !retryConnection() ) ; + while (!check && !retryConnection()) ; + keyboardReader.setTopicConn(topicConn); @@ -180,6 +184,7 @@ public void receive(){ e1.printStackTrace(); } while (!check && !retryConnection() ) ; + keyboardReader.setTopicConn(topicConn); } @@ -198,7 +203,7 @@ public void produceStats(){ hashmap.put("Latin America", 0); hashmap.put("Middle East", 0); - + //filtra mensagens com mais de 12 horas for (Iterator
iterator = listaNoticias.getArticle().iterator(); iterator.hasNext();) { Article a = iterator.next(); @@ -212,7 +217,7 @@ public void produceStats(){ interval=TimeUnit.MILLISECONDS.toMinutes(interval); System.out.println("Titulo: "+a.getTitle()+" section:"+a.getSection()+" data: "+startDate+" intervalo: "+interval); if (interval>12*60) { - // Remove the current element from the iterator and the list. + iterator.remove(); } } @@ -220,8 +225,14 @@ public void produceStats(){ int numberNews=listaNoticias.getArticle().size(); System.out.println(numberNews); for(Article a:listaNoticias.getArticle()){ + if(hashmap.containsKey(a.getSection())) hashmap.put(a.getSection(),hashmap.get(a.getSection())+1); + else{ + if(a.getSection().compareTo("")!=0){ + hashmap.put(a.getSection(),1); + } + } }