Restore worker exceptions from 9094efb80b
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
package mimis.exception;
|
||||||
|
|
||||||
|
public class WorkerException extends Exception {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class ActivateException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class AlreadyActiveException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class AlreadyRunningException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class DeactivateException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class NotActiveException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class NotRunningException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package mimis.exception.worker;
|
||||||
|
|
||||||
|
import mimis.exception.WorkerException;
|
||||||
|
|
||||||
|
public class StartException extends WorkerException {
|
||||||
|
protected static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
81
java/mimis/src/main/java/mimis/worker/IntervalWorker.java
Normal file
81
java/mimis/src/main/java/mimis/worker/IntervalWorker.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package mimis.worker;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import mimis.exception.worker.ActivateException;
|
||||||
|
import mimis.exception.worker.DeactivateException;
|
||||||
|
|
||||||
|
public class IntervalWorker extends Worker {
|
||||||
|
protected static final boolean THREAD = true;
|
||||||
|
protected static final int INTERVAL = 500;
|
||||||
|
|
||||||
|
protected Timer timer;
|
||||||
|
|
||||||
|
public synchronized void start(boolean thread) {
|
||||||
|
if (!active) {
|
||||||
|
activate = true;
|
||||||
|
timer = new Timer();
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
public void run() {
|
||||||
|
IntervalWorker.this.run();
|
||||||
|
}}, 0, INTERVAL);
|
||||||
|
active = true;
|
||||||
|
}
|
||||||
|
if (!thread) {
|
||||||
|
try {
|
||||||
|
synchronized (this) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.info("", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void stop() {
|
||||||
|
if (active) {
|
||||||
|
timer.cancel();
|
||||||
|
deactivate = true;
|
||||||
|
run();
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
if (activate && !active) {
|
||||||
|
try {
|
||||||
|
super.activate();
|
||||||
|
} catch (ActivateException e) {
|
||||||
|
logger.error("", e);
|
||||||
|
} finally {
|
||||||
|
activate = false;
|
||||||
|
}
|
||||||
|
} else if (deactivate && active) {
|
||||||
|
try {
|
||||||
|
super.deactivate();
|
||||||
|
} catch (DeactivateException e) {
|
||||||
|
logger.error("", e);
|
||||||
|
} finally {
|
||||||
|
deactivate = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (active) {
|
||||||
|
work();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void work() {
|
||||||
|
System.out.println("(-:");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IntervalWorker intervalWorker = new IntervalWorker();
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
intervalWorker.start(false);
|
||||||
|
System.out.println("--");
|
||||||
|
intervalWorker.sleep(200);
|
||||||
|
intervalWorker.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
java/mimis/src/main/java/mimis/worker/Listener.java
Normal file
35
java/mimis/src/main/java/mimis/worker/Listener.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package mimis.worker;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class Listener<E> extends Worker {
|
||||||
|
protected Queue<E> queue;
|
||||||
|
|
||||||
|
public Listener() {
|
||||||
|
queue = new ConcurrentLinkedQueue<E>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void add(E element) {
|
||||||
|
queue.add(element);
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void work() {
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
input(queue.poll());
|
||||||
|
}
|
||||||
|
if (!deactivate) {
|
||||||
|
synchronized (this) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.info("", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void input(E element);
|
||||||
|
}
|
||||||
123
java/mimis/src/main/java/mimis/worker/Worker.java
Normal file
123
java/mimis/src/main/java/mimis/worker/Worker.java
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
package mimis.worker;
|
||||||
|
|
||||||
|
import mimis.exception.worker.ActivateException;
|
||||||
|
import mimis.exception.worker.DeactivateException;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public abstract class Worker implements Runnable {
|
||||||
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
protected static final boolean THREAD = true;
|
||||||
|
protected static final int SLEEP = 100;
|
||||||
|
|
||||||
|
protected boolean thread = true;
|
||||||
|
protected boolean run = false;
|
||||||
|
protected boolean active = false;
|
||||||
|
protected boolean activate = false;
|
||||||
|
protected boolean deactivate = false;
|
||||||
|
|
||||||
|
public Worker(boolean thread) {
|
||||||
|
this.thread = thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Worker() {
|
||||||
|
this(THREAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void start(boolean thread) {
|
||||||
|
if (!active) {
|
||||||
|
activate = true;
|
||||||
|
}
|
||||||
|
if (!run) {
|
||||||
|
run = true;
|
||||||
|
if (thread) {
|
||||||
|
logger.debug("Start thread");
|
||||||
|
new Thread(this, getClass().getName()).start();
|
||||||
|
} else {
|
||||||
|
logger.debug("Run directly");
|
||||||
|
run();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void start() {
|
||||||
|
start(thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void stop() {
|
||||||
|
if (active) {
|
||||||
|
deactivate = true;
|
||||||
|
}
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exit() {
|
||||||
|
stop();
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sleep(int time) {
|
||||||
|
try {
|
||||||
|
if (time > 0) {
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.info("", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sleep() {
|
||||||
|
sleep(SLEEP);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean active() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void activate() throws ActivateException {
|
||||||
|
active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate() throws DeactivateException {
|
||||||
|
active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (run || deactivate) {
|
||||||
|
if (activate && !active) {
|
||||||
|
try {
|
||||||
|
activate();
|
||||||
|
} catch (ActivateException e) {
|
||||||
|
logger.error("", e);
|
||||||
|
} finally {
|
||||||
|
activate = false;
|
||||||
|
}
|
||||||
|
} else if (deactivate && active) {
|
||||||
|
try {
|
||||||
|
deactivate();
|
||||||
|
} catch (DeactivateException e) {
|
||||||
|
logger.error("", e);
|
||||||
|
} finally {
|
||||||
|
deactivate = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (active) {
|
||||||
|
work();
|
||||||
|
} else if (run) {
|
||||||
|
try {
|
||||||
|
synchronized (this) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.info("", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void work();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user