Migrate to advanced worker model for worker/listener/timer using direct/thread/pool executor
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
|
repositories {
|
||||||
|
mavenCentral();
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'org.slf4j:slf4j-api:1.7.7'
|
compile 'org.slf4j:slf4j-api:1.7.7'
|
||||||
compile 'org.slf4j:slf4j-log4j12:1.7.7'
|
compile 'org.slf4j:slf4j-log4j12:1.7.7'
|
||||||
|
compile 'org.picocontainer:picocontainer:2.15'
|
||||||
|
compile 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
@@ -4,9 +4,9 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
import base.worker.Worker;
|
import base.worker.ThreadWorker;
|
||||||
|
|
||||||
public abstract class AbstractClient extends Worker {
|
public abstract class AbstractClient extends ThreadWorker {
|
||||||
protected Socket socket;
|
protected Socket socket;
|
||||||
protected InputStream inputStream;
|
protected InputStream inputStream;
|
||||||
protected OutputStream outputStream;
|
protected OutputStream outputStream;
|
||||||
|
|||||||
61
java/base/src/main/java/base/work/Listen.java
Normal file
61
java/base/src/main/java/base/work/Listen.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package base.work;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
import base.worker.DirectListener;
|
||||||
|
import base.worker.ThreadListener;
|
||||||
|
import base.worker.Worker;
|
||||||
|
import base.worker.pool.Listener;
|
||||||
|
import base.worker.pool.ListenerPool;
|
||||||
|
import base.worker.pool.PooledListener;
|
||||||
|
|
||||||
|
public abstract class Listen<E> extends Work {
|
||||||
|
protected Listener<E> listener;
|
||||||
|
public Queue<E> queue;
|
||||||
|
|
||||||
|
public Listen() {
|
||||||
|
this(WORKER_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Listen(Worker.Type workerType) {
|
||||||
|
switch (workerType) {
|
||||||
|
case DIRECT:
|
||||||
|
listener = new DirectListener<E>(this);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case THREAD:
|
||||||
|
listener = new ThreadListener<E>(this);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
queue = new ConcurrentLinkedQueue<E>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Listen(Worker worker) {
|
||||||
|
this.work = worker;
|
||||||
|
queue = new ConcurrentLinkedQueue<E>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Listen(ListenerPool<E> listenerPool) {
|
||||||
|
listener = new PooledListener<E>(this);
|
||||||
|
listenerPool.add((PooledListener<E>) listener);
|
||||||
|
queue = new ConcurrentLinkedQueue<E>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void add(E element) {
|
||||||
|
listener.add(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void work() {
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
input(queue.poll());
|
||||||
|
}
|
||||||
|
synchronized (this) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void input(E element) {}
|
||||||
|
}
|
||||||
56
java/base/src/main/java/base/work/Work.java
Normal file
56
java/base/src/main/java/base/work/Work.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package base.work;
|
||||||
|
|
||||||
|
import base.exception.worker.ActivateException;
|
||||||
|
import base.exception.worker.DeactivateException;
|
||||||
|
import base.worker.DirectWorker;
|
||||||
|
import base.worker.ThreadWorker;
|
||||||
|
import base.worker.Worker;
|
||||||
|
import base.worker.pool.PooledWorker;
|
||||||
|
import base.worker.pool.WorkerPool;
|
||||||
|
|
||||||
|
public abstract class Work {
|
||||||
|
protected static final Worker.Type WORKER_TYPE = Worker.Type.THREAD;
|
||||||
|
|
||||||
|
protected Worker work;
|
||||||
|
|
||||||
|
protected Work() {
|
||||||
|
this(WORKER_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Work(Worker.Type workerType) {
|
||||||
|
switch (workerType) {
|
||||||
|
case DIRECT:
|
||||||
|
work = new DirectWorker(this);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case THREAD:
|
||||||
|
work = new ThreadWorker(this);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Work(Worker worker) {
|
||||||
|
this.work = worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Work(WorkerPool workerPool) {
|
||||||
|
work = new PooledWorker(this);
|
||||||
|
workerPool.add((PooledWorker) work);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sleep(int time) {
|
||||||
|
work.sleep(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
work.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
work.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void activate() throws ActivateException {}
|
||||||
|
public void deactivate() throws DeactivateException {}
|
||||||
|
public abstract void work();
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package base.worker;
|
|
||||||
|
|
||||||
public abstract class AbstractWorker {
|
|
||||||
public abstract void start();
|
|
||||||
|
|
||||||
public abstract void stop();
|
|
||||||
|
|
||||||
protected abstract void work();
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public class DirectIntervalWorker extends ThreadIntervalWorker {
|
||||||
|
public DirectIntervalWorker(Work work, int interval) {
|
||||||
|
super(work, false);
|
||||||
|
this.interval = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DirectIntervalWorker(IntervalWork intervalWork) {
|
||||||
|
super(intervalWork);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
java/base/src/main/java/base/worker/DirectListener.java
Normal file
16
java/base/src/main/java/base/worker/DirectListener.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Listen;
|
||||||
|
import base.worker.pool.Listener;
|
||||||
|
|
||||||
|
public class DirectListener<E> extends ThreadListener<E> implements Listener<E> {
|
||||||
|
public DirectListener(Listen<E> listen) {
|
||||||
|
super(listen, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Object element) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
9
java/base/src/main/java/base/worker/DirectWorker.java
Normal file
9
java/base/src/main/java/base/worker/DirectWorker.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public class DirectWorker extends ThreadWorker {
|
||||||
|
public DirectWorker(Work work) {
|
||||||
|
super(work, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
37
java/base/src/main/java/base/worker/IntervalWork.java
Normal file
37
java/base/src/main/java/base/worker/IntervalWork.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public abstract class IntervalWork extends Work {
|
||||||
|
protected IntervalWork() {
|
||||||
|
this(WORKER_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IntervalWork(int interval) {
|
||||||
|
this(WORKER_TYPE, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IntervalWork(Worker.Type workerType) {
|
||||||
|
switch (workerType) {
|
||||||
|
case DIRECT:
|
||||||
|
work = new DirectIntervalWorker(this);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case THREAD:
|
||||||
|
work = new ThreadIntervalWorker(this);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IntervalWork(Worker.Type workerType, int interval) {
|
||||||
|
switch (workerType) {
|
||||||
|
case DIRECT:
|
||||||
|
work = new DirectIntervalWorker(this, interval);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case THREAD:
|
||||||
|
work = new ThreadIntervalWorker(this, interval);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
package base.worker;
|
|
||||||
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import base.exception.worker.ActivateException;
|
|
||||||
import base.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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package base.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);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public class ThreadIntervalWorker extends ThreadWorker {
|
||||||
|
protected static final int INTERVAL = 500;
|
||||||
|
protected int interval;
|
||||||
|
|
||||||
|
public ThreadIntervalWorker(Work work) {
|
||||||
|
super(work);
|
||||||
|
interval = INTERVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ThreadIntervalWorker(Work work, boolean thread) {
|
||||||
|
super(work, thread);
|
||||||
|
interval = INTERVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ThreadIntervalWorker(Work work, int interval) {
|
||||||
|
super(work);
|
||||||
|
this.interval = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Timer timer;
|
||||||
|
|
||||||
|
public synchronized void start(boolean thread) {
|
||||||
|
if (!active) {
|
||||||
|
activate = true;
|
||||||
|
timer = new Timer();
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
public void run() {
|
||||||
|
Worker worker = ThreadIntervalWorker.this;
|
||||||
|
worker.runActivate();
|
||||||
|
worker.runDeactivate();
|
||||||
|
worker.runWork();
|
||||||
|
}}, 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
java/base/src/main/java/base/worker/ThreadListener.java
Normal file
22
java/base/src/main/java/base/worker/ThreadListener.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Listen;
|
||||||
|
import base.worker.pool.Listener;
|
||||||
|
|
||||||
|
public class ThreadListener<E> extends ThreadWorker implements Listener<E> {
|
||||||
|
protected Listen<E> listen;
|
||||||
|
|
||||||
|
public ThreadListener(Listen<E> listen) {
|
||||||
|
super(listen);
|
||||||
|
this.listen = listen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ThreadListener(Listen<E> listen, boolean start) {
|
||||||
|
super(listen);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(E element) {
|
||||||
|
System.out.println("ok! " + element);
|
||||||
|
listen.queue.add(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
52
java/base/src/main/java/base/worker/ThreadWorker.java
Normal file
52
java/base/src/main/java/base/worker/ThreadWorker.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package base.worker;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public class ThreadWorker extends Worker implements Runnable {
|
||||||
|
protected static final boolean THREAD = true;
|
||||||
|
|
||||||
|
protected boolean thread = true;
|
||||||
|
|
||||||
|
public ThreadWorker(Work work, boolean thread) {
|
||||||
|
this(work);
|
||||||
|
this.thread = thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ThreadWorker(Work work) {
|
||||||
|
super(work);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,108 +5,73 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import base.exception.worker.ActivateException;
|
import base.exception.worker.ActivateException;
|
||||||
import base.exception.worker.DeactivateException;
|
import base.exception.worker.DeactivateException;
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
public abstract class Worker implements Runnable {
|
public abstract class Worker {
|
||||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
public enum Type {
|
||||||
|
DIRECT, THREAD, POOLED
|
||||||
|
}
|
||||||
|
|
||||||
protected static final boolean THREAD = true;
|
|
||||||
protected static final int SLEEP = 100;
|
protected static final int SLEEP = 100;
|
||||||
|
|
||||||
protected boolean thread = true;
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
protected boolean run = false;
|
protected boolean run = false;
|
||||||
protected boolean active = false;
|
protected boolean active = false;
|
||||||
protected boolean activate = false;
|
protected boolean activate = false;
|
||||||
protected boolean deactivate = false;
|
protected boolean deactivate = false;
|
||||||
|
|
||||||
public Worker(boolean thread) {
|
protected Work work;
|
||||||
this.thread = thread;
|
|
||||||
|
public Worker(Work work) {
|
||||||
|
this.work = work;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Worker() {
|
public abstract void start();
|
||||||
this(THREAD);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void start(boolean thread) {
|
public abstract void stop();
|
||||||
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() {
|
public boolean active() {
|
||||||
return active;
|
return active;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void activate() throws ActivateException {
|
|
||||||
active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void deactivate() throws DeactivateException {
|
|
||||||
active = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
while (run || deactivate) {
|
while (run || deactivate) {
|
||||||
|
runActivate();
|
||||||
|
runDeactivate();
|
||||||
|
runWork();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runActivate() {
|
||||||
if (activate && !active) {
|
if (activate && !active) {
|
||||||
try {
|
try {
|
||||||
activate();
|
work.activate();
|
||||||
} catch (ActivateException e) {
|
} catch (ActivateException e) {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
} finally {
|
} finally {
|
||||||
activate = false;
|
activate = false;
|
||||||
|
active = true;
|
||||||
}
|
}
|
||||||
} else if (deactivate && active) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runDeactivate() {
|
||||||
|
if (deactivate && active) {
|
||||||
try {
|
try {
|
||||||
deactivate();
|
work.deactivate();
|
||||||
} catch (DeactivateException e) {
|
} catch (DeactivateException e) {
|
||||||
logger.error("", e);
|
logger.error("", e);
|
||||||
} finally {
|
} finally {
|
||||||
deactivate = false;
|
deactivate = false;
|
||||||
|
active = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runWork() {
|
||||||
if (active) {
|
if (active) {
|
||||||
work();
|
work.work();
|
||||||
} else if (run) {
|
} else if (run) {
|
||||||
try {
|
try {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
@@ -117,7 +82,18 @@ public abstract class Worker implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sleep() {
|
||||||
|
sleep(SLEEP);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void work();
|
public void sleep(int time) {
|
||||||
|
try {
|
||||||
|
if (time > 0) {
|
||||||
|
Thread.sleep(time);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.info("", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
java/base/src/main/java/base/worker/pool/Listener.java
Normal file
5
java/base/src/main/java/base/worker/pool/Listener.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package base.worker.pool;
|
||||||
|
|
||||||
|
public interface Listener<E> {
|
||||||
|
public void add(E element);
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.SynchronousQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ListenerPool<E> {
|
public class ListenerPool<E> {
|
||||||
@@ -13,7 +13,7 @@ public class ListenerPool<E> {
|
|||||||
|
|
||||||
public ListenerPool(int poolSize) {
|
public ListenerPool(int poolSize) {
|
||||||
this.poolSize = poolSize;
|
this.poolSize = poolSize;
|
||||||
queue = new SynchronousQueue<Wrapper<E>>();
|
queue = new LinkedBlockingQueue<Wrapper<E>>();
|
||||||
executorService = Executors.newFixedThreadPool(poolSize);
|
executorService = Executors.newFixedThreadPool(poolSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
28
java/base/src/main/java/base/worker/pool/PooledListener.java
Normal file
28
java/base/src/main/java/base/worker/pool/PooledListener.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package base.worker.pool;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
import base.work.Listen;
|
||||||
|
|
||||||
|
public class PooledListener<E> extends PooledWorker implements Listener<E> {
|
||||||
|
protected BlockingQueue<Wrapper<E>> poolQueue;
|
||||||
|
protected Listen<E> listen;
|
||||||
|
|
||||||
|
public PooledListener(Listen<E> listen) {
|
||||||
|
super(listen);
|
||||||
|
this.listen = listen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPoolQueue(BlockingQueue<Wrapper<E>> poolQueue) {
|
||||||
|
this.poolQueue = poolQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void add(E element) {
|
||||||
|
Wrapper<E> wrapper = new Wrapper<E>(this, element);
|
||||||
|
poolQueue.add(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
void input(E element) {
|
||||||
|
listen.input(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,18 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
import base.work.Work;
|
||||||
import base.worker.Worker;
|
import base.worker.Worker;
|
||||||
|
|
||||||
public abstract class PooledWorker extends Worker {
|
public class PooledWorker extends Worker {
|
||||||
protected BlockingQueue<Worker> activateQueue;
|
protected BlockingQueue<Worker> activateQueue;
|
||||||
protected BlockingQueue<Worker> deactivateQueue;
|
protected BlockingQueue<Worker> deactivateQueue;
|
||||||
|
|
||||||
|
public PooledWorker(Work work) {
|
||||||
|
super(work);
|
||||||
|
}
|
||||||
|
|
||||||
public void setActivateQueue(BlockingQueue<Worker> activateQueue) {
|
public void setActivateQueue(BlockingQueue<Worker> activateQueue) {
|
||||||
this.activateQueue = activateQueue;
|
this.activateQueue = activateQueue;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ public class WorkerPoolRunnable implements Runnable {
|
|||||||
} catch (InterruptedException e) {}
|
} catch (InterruptedException e) {}
|
||||||
} else {
|
} else {
|
||||||
Worker worker = workerCycle.next();
|
Worker worker = workerCycle.next();
|
||||||
worker.work();
|
worker.runWork();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package base.worker.pooled;
|
package base.worker.pool;
|
||||||
|
|
||||||
|
|
||||||
class Wrapper<E> {
|
class Wrapper<E> {
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package base.worker.pooled;
|
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
|
||||||
|
|
||||||
public abstract class PooledListener<E> {
|
|
||||||
protected BlockingQueue<Wrapper<E>> poolQueue;
|
|
||||||
|
|
||||||
public void setPoolQueue(BlockingQueue<Wrapper<E>> poolQueue) {
|
|
||||||
this.poolQueue = poolQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void add(E element) {
|
|
||||||
Wrapper<E> wrapper = new Wrapper<E>(this, element);
|
|
||||||
poolQueue.add(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract void input(E element);
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
package base.worker.pooled;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import base.exception.worker.ActivateException;
|
|
||||||
import base.exception.worker.DeactivateException;
|
|
||||||
import base.worker.Worker;
|
|
||||||
|
|
||||||
public class TestPooledWorker extends PooledWorker {
|
|
||||||
protected int id;
|
|
||||||
protected volatile int work;
|
|
||||||
|
|
||||||
public TestPooledWorker(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setWork(int work) {
|
|
||||||
System.out.println("#" + id + ", set work @ " + work);
|
|
||||||
this.work = work;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void work() {
|
|
||||||
System.out.println("#" + id + ", work = " + work);
|
|
||||||
if (--work < 1) {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
sleep(300);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void activate() throws ActivateException {
|
|
||||||
System.out.println("#" + id + ", activating...");
|
|
||||||
super.activate();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void deactivate() throws DeactivateException {
|
|
||||||
super.deactivate();
|
|
||||||
System.out.println("#" + id + ", deactivating...");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
WorkerPool workerPool = new WorkerPool(3);
|
|
||||||
|
|
||||||
List<TestPooledWorker> workerList = new ArrayList<TestPooledWorker>();
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
TestPooledWorker worker = new TestPooledWorker(i + 1);
|
|
||||||
workerPool.add(worker);
|
|
||||||
workerList.add(worker);
|
|
||||||
}
|
|
||||||
workerPool.start();
|
|
||||||
|
|
||||||
System.out.println("Starting work!");
|
|
||||||
int work = 1000;
|
|
||||||
ArrayList<Worker> activeWorkerList = new ArrayList<Worker>();
|
|
||||||
for (int i = 0; i < 8; ++i) {
|
|
||||||
TestPooledWorker worker = workerList.get((new Random()).nextInt(workerList.size()));
|
|
||||||
worker.setWork(work);
|
|
||||||
worker.start();
|
|
||||||
activeWorkerList.add(worker);
|
|
||||||
}
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(2000);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
|
|
||||||
for (Worker worker : activeWorkerList) {
|
|
||||||
if (++i > 5) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
worker.stop();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Thread.sleep(100000);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
15
java/base/src/test/java/worker/TestDirectWork.java
Normal file
15
java/base/src/test/java/worker/TestDirectWork.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package worker;
|
||||||
|
|
||||||
|
import worker.dummy.DummyWork;
|
||||||
|
|
||||||
|
|
||||||
|
public class TestDirectWork {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
DummyWork work = new DummyWork(1);
|
||||||
|
work.setWork(100);
|
||||||
|
work.start();
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
java/base/src/test/java/worker/TestIntervalWork.java
Normal file
18
java/base/src/test/java/worker/TestIntervalWork.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package worker;
|
||||||
|
|
||||||
|
import worker.dummy.DummyIntervalWork;
|
||||||
|
import base.work.Work;
|
||||||
|
|
||||||
|
public class TestIntervalWork {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Work work = new DummyIntervalWork(500);
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
work.start();
|
||||||
|
System.out.println("--");
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
work.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
java/base/src/test/java/worker/TestListen.java
Normal file
19
java/base/src/test/java/worker/TestListen.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package worker;
|
||||||
|
|
||||||
|
import worker.dummy.DummyListen;
|
||||||
|
|
||||||
|
public class TestListen {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
DummyListen listen = new DummyListen(0);
|
||||||
|
listen.start();
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
listen.add(i);
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,16 @@
|
|||||||
package base.worker.pooled;
|
package worker;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class TestPooledListener extends PooledListener<Integer> {
|
import worker.dummy.DummyListen;
|
||||||
|
import base.worker.pool.ListenerPool;
|
||||||
|
|
||||||
|
public class TestPooledListen {
|
||||||
protected int id;
|
protected int id;
|
||||||
|
|
||||||
public TestPooledListener(int id) {
|
public TestPooledListen(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,18 +20,17 @@ public class TestPooledListener extends PooledListener<Integer> {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ListenerPool<Integer> listenerPool = new ListenerPool<Integer>(5);
|
ListenerPool<Integer> listenerPool = new ListenerPool<Integer>(5);
|
||||||
List<PooledListener<Integer>> listenerList = new ArrayList<PooledListener<Integer>>();
|
List<DummyListen> listenList = new ArrayList<DummyListen>();
|
||||||
for (int i = 0; i < 20; ++i) {
|
for (int i = 0; i < 20; ++i) {
|
||||||
PooledListener<Integer> pooledListener = listenerPool.add(new TestPooledListener(i + 1));
|
DummyListen listen = new DummyListen(listenerPool, i + 1);
|
||||||
listenerList.add(pooledListener);
|
listenList.add(listen);
|
||||||
}
|
}
|
||||||
|
|
||||||
listenerPool.start();
|
listenerPool.start();
|
||||||
|
|
||||||
System.out.println("Starting to give out elements!");
|
System.out.println("Starting to give out elements!");
|
||||||
for (int i = 0; i < 100; ++i) {
|
for (int i = 0; i < 100; ++i) {
|
||||||
PooledListener<Integer> randomListener = listenerList.get((new Random()).nextInt(listenerList.size()));
|
DummyListen randomListen = listenList.get((new Random()).nextInt(listenList.size()));
|
||||||
randomListener.add(i);
|
randomListen.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
//listenerPool.await();
|
//listenerPool.await();
|
||||||
45
java/base/src/test/java/worker/TestPooledWork.java
Normal file
45
java/base/src/test/java/worker/TestPooledWork.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package worker;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import worker.dummy.DummyWork;
|
||||||
|
import base.work.Work;
|
||||||
|
import base.worker.pool.WorkerPool;
|
||||||
|
|
||||||
|
public class TestPooledWork {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
WorkerPool workerPool = new WorkerPool(3);
|
||||||
|
|
||||||
|
List<DummyWork> workList = new ArrayList<DummyWork>();
|
||||||
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
DummyWork work = new DummyWork(workerPool, i + 1);
|
||||||
|
workList.add(work);
|
||||||
|
}
|
||||||
|
workerPool.start();
|
||||||
|
|
||||||
|
System.out.println("Starting work!");
|
||||||
|
ArrayList<Work> activeWorkList = new ArrayList<Work>();
|
||||||
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
DummyWork work = workList.get((new Random()).nextInt(workList.size()));
|
||||||
|
work.setWork(1000);
|
||||||
|
work.start();
|
||||||
|
activeWorkList.add(work);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
int i = 0;
|
||||||
|
for (Work work : activeWorkList) {
|
||||||
|
if (++i > 5) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
work.stop();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(100000);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
java/base/src/test/java/worker/dummy/DummyIntervalWork.java
Normal file
13
java/base/src/test/java/worker/dummy/DummyIntervalWork.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package worker.dummy;
|
||||||
|
|
||||||
|
import base.worker.IntervalWork;
|
||||||
|
|
||||||
|
public class DummyIntervalWork extends IntervalWork {
|
||||||
|
public DummyIntervalWork(int interval) {
|
||||||
|
super(interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void work() {
|
||||||
|
System.out.println(":-)");
|
||||||
|
}
|
||||||
|
}
|
||||||
22
java/base/src/test/java/worker/dummy/DummyListen.java
Normal file
22
java/base/src/test/java/worker/dummy/DummyListen.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package worker.dummy;
|
||||||
|
|
||||||
|
import base.work.Listen;
|
||||||
|
import base.worker.pool.ListenerPool;
|
||||||
|
|
||||||
|
public class DummyListen extends Listen<Integer> {
|
||||||
|
protected int id;
|
||||||
|
|
||||||
|
public DummyListen(ListenerPool<Integer> listenerPool, int id) {
|
||||||
|
super(listenerPool);
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DummyListen(int id) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void input(Integer input) {
|
||||||
|
System.out.println("#" + id + ", input = " + input);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
java/base/src/test/java/worker/dummy/DummyWork.java
Normal file
42
java/base/src/test/java/worker/dummy/DummyWork.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package worker.dummy;
|
||||||
|
|
||||||
|
import base.exception.worker.ActivateException;
|
||||||
|
import base.exception.worker.DeactivateException;
|
||||||
|
import base.work.Work;
|
||||||
|
import base.worker.pool.WorkerPool;
|
||||||
|
|
||||||
|
public class DummyWork extends Work {
|
||||||
|
protected int id;
|
||||||
|
protected volatile int work;
|
||||||
|
|
||||||
|
public DummyWork(int id) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DummyWork(WorkerPool workerPool, int id) {
|
||||||
|
super(workerPool);
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWork(int work) {
|
||||||
|
System.out.println("#" + id + ", set work @ " + work);
|
||||||
|
this.work = work;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void work() {
|
||||||
|
System.out.println("#" + id + ", work = " + work);
|
||||||
|
if (--work < 1) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
sleep(300);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void activate() throws ActivateException {
|
||||||
|
System.out.println("#" + id + ", activating...");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deactivate() throws DeactivateException {
|
||||||
|
System.out.println("#" + id + ", deactivating...");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user