Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions Crawler/src/JMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,17 @@ 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);

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());
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Crawler/src/NewsCrawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class NewsCrawler implements Runnable {
public NewsCrawler() {
this.pool = new LinkedBlockingQueue<String>();
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();
}
Expand Down Expand Up @@ -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())));
Expand Down Expand Up @@ -175,7 +175,7 @@ public static void main(String[] args) throws InterruptedException {
}

if (crawler != null) {
crawler.loadFailed();
//crawler.loadFailed();
(new Thread(crawler)).start();

}
Expand Down
16 changes: 11 additions & 5 deletions Stats Producer/src/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
/**
* 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) {
Scanner sc = new Scanner(System.in);
if (sc.nextLine().compareTo("exit") == 0) {
check = true;
try {
connection.close();
topicConn.close();



System.exit(0);
Expand All @@ -38,4 +42,6 @@ public void run(){
}
}
}


}
37 changes: 24 additions & 13 deletions Stats Producer/src/StatsProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public class StatsProducer extends Thread {
NewsList listaNoticias=new NewsList();
HashMap<String,Integer> hashmap = new HashMap<String, Integer>();
Reader keyboardReader;
TopicSubscriber topicSubscriber;
InitialContext ctx;
Topic topic;
Expand All @@ -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(){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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();



Expand Down Expand Up @@ -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);
Expand All @@ -169,7 +172,8 @@ public void receive(){
} catch (InterruptedException e1) {
e1.printStackTrace();
}
while (!check && !retryConnection() ) ;
while (!check && !retryConnection()) ;
keyboardReader.setTopicConn(topicConn);



Expand All @@ -180,6 +184,7 @@ public void receive(){
e1.printStackTrace();
}
while (!check && !retryConnection() ) ;
keyboardReader.setTopicConn(topicConn);


}
Expand All @@ -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<Article> iterator = listaNoticias.getArticle().iterator(); iterator.hasNext();) {
Article a = iterator.next();

Expand All @@ -212,16 +217,22 @@ 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();
}
}

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);
}
}


}
Expand Down