gr.iti.mklab.sfc.management.Consumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mklab-stream-manager Show documentation
Show all versions of mklab-stream-manager Show documentation
Monitors a set of social streams (e.g. Twitter status updates) and collects the incoming content.
The newest version!
package gr.iti.mklab.sfc.management;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import gr.iti.mklab.framework.common.domain.Item;
import gr.iti.mklab.sfc.filters.ItemFilter;
import gr.iti.mklab.sfc.processors.Processor;
import gr.iti.mklab.sfc.storages.Storage;
/**
* Class for storing items to databases
*
*
* @author manosetro - [email protected]
*
*/
public class Consumer extends Thread {
private Logger _logger = LogManager.getLogger(Consumer.class);
private static int id = 0;
private boolean isAlive = true;
private List storages = null;
private BlockingQueue- queue;
private Collection
filters;
private Collection processors;
private long lastAccess = 0;
private long itemsConsumed = 0L;
private int lastAction = 0;
private String[] actions = {"initialization", "taking from queue", "filtering", "running processors", "storing", " handling"};
public Consumer(BlockingQueue- queue, List
storages, Collection filters, Collection processors) {
this.storages = storages;
this.queue = queue;
this.filters = filters;
this.processors = processors;
this.setName("Consumer_" + (id++));
}
/**
* Stores an item if the latter is found waiting in the queue
*/
public void run() {
Item item = null;
while (isAlive) {
try {
item = queue.take();
if (item == null) {
_logger.error("Item is null.");
}
else {
lastAction = 1;
lastAccess = System.currentTimeMillis();
itemsConsumed++;
process(item);
}
} catch(IOException e) {
_logger.error(e);
} catch (InterruptedException e) {
_logger.error(e);
}
}
//empty queue before exit
while ((item = queue.poll()) != null) {
try {
process(item);
} catch (IOException e) {
_logger.error(e);
}
}
}
private void process(Item item) throws IOException {
if (storages != null) {
for(ItemFilter filter : filters) {
boolean accept = true;
synchronized(filter) {
accept = filter.accept(item);
}
if(!accept) {
return;
}
}
lastAction = 2;
for(Processor processor : processors) {
synchronized(processor) {
processor.process(item);
}
}
lastAction = 3;
for(Storage storage : storages) {
synchronized(storage) {
storage.store(item);
}
}
lastAction = 4;
}
else {
_logger.error("Sorages list in null. Cannot process item.");
}
}
/**
* Stops the consumer thread
*/
public synchronized void die() {
isAlive = false;
try {
this.interrupt();
}
catch(Exception e) {
_logger.error(e);
}
}
private Date getLastAccess() {
return new Date(lastAccess);
}
private long getConsumedItems() {
return itemsConsumed;
}
public String status() {
return getName() + " consumed " + getConsumedItems() + ". Last access [" + getLastAccess()
+ "]. Current state: " + getState() + " after " + actions[lastAction];
}
}