Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

Commit

Permalink
app: add mapper on msg
Browse files Browse the repository at this point in the history
  • Loading branch information
T4ze committed Dec 9, 2017
1 parent 86ce8aa commit e365344
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/epita/guereza/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.epita.eventbus.EventBusClient;
import com.epita.eventbus.EventMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -48,4 +49,13 @@ protected void retryIn(final int seconds, Runnable consumer) {
executor.schedule(consumer, seconds, TimeUnit.SECONDS);
executor.shutdownNow();
}

protected Object mappingObject(EventBusClient.Message message) {
try {
Class c = ClassLoader.getSystemClassLoader().loadClass(message.getMessageType());
return new ObjectMapper().readValue(message.getContent(), c);
} catch (Exception e) {
return null;
}
}
}
8 changes: 2 additions & 6 deletions src/main/java/com/epita/guereza/CrawlerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ private void storeUrls(final String[] urls) {
public void run() {
eventBus.subscribe(subscribeUrl, msg -> {
if (msg != null) {
try {
Class c = ClassLoader.getSystemClassLoader().loadClass(msg.getMessageType());
String url = (String)new ObjectMapper().readValue(msg.getContent(), c);
String url = (String)mappingObject(msg);
if (url != null) {
LOGGER.info("Receive url: {}", url);
final String[] urls = crawlAndExtract(url);
storeUrls(urls);
} catch (Exception e) {
e.printStackTrace();
}


requestNextUrl();
} else {
retryIn(30, this::requestNextUrl);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/epita/guereza/IndexerApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ private void requestNextUrl() {
public void run() {
eventBus.subscribe(subscribeUrl, msg -> {
if (msg != null) {
LOGGER.info("Receive url: {}", msg.getContent());
indexAndPublish(msg.getContent());
String url = (String)mappingObject(msg);
if (url != null) {
LOGGER.info("Receive url: {}", url);
indexAndPublish(url);
}

requestNextUrl();
} else {
Expand Down

0 comments on commit e365344

Please sign in to comment.