Move all files from java subdirectory to project root
This commit is contained in:
5
core.worker/build.gradle
Normal file
5
core.worker/build.gradle
Normal file
@@ -0,0 +1,5 @@
|
||||
dependencies {
|
||||
compile 'org.slf4j:slf4j-log4j12:1.7.7'
|
||||
compile 'org.picocontainer:picocontainer:2.15'
|
||||
compile 'junit:junit:4.12'
|
||||
}
|
||||
BIN
core.worker/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
core.worker/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
6
core.worker/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
6
core.worker/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
#Mon Jul 11 22:59:09 BST 2016
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
|
||||
164
core.worker/gradlew
vendored
Normal file
164
core.worker/gradlew
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn ( ) {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die ( ) {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
||||
function splitJvmOpts() {
|
||||
JVM_OPTS=("$@")
|
||||
}
|
||||
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
||||
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
||||
|
||||
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
||||
90
core.worker/gradlew.bat
vendored
Normal file
90
core.worker/gradlew.bat
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS=
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
if "%@eval[2+2]" == "4" goto 4NT_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
goto execute
|
||||
|
||||
:4NT_args
|
||||
@rem Get arguments from the 4NT Shell from JP Software
|
||||
set CMD_LINE_ARGS=%$
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
23
core.worker/src/main/java/base/Control.java
Normal file
23
core.worker/src/main/java/base/Control.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base;
|
||||
|
||||
public interface Control {
|
||||
public void start();
|
||||
public void stop();
|
||||
public void exit();
|
||||
}
|
||||
19
core.worker/src/main/java/base/Duplex.java
Normal file
19
core.worker/src/main/java/base/Duplex.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base;
|
||||
|
||||
public interface Duplex extends Forwarder, Sender {}
|
||||
22
core.worker/src/main/java/base/Forwarder.java
Normal file
22
core.worker/src/main/java/base/Forwarder.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base;
|
||||
|
||||
public interface Forwarder extends Control {
|
||||
public void register(Receiver receiver);
|
||||
public void remove(Receiver receiver);
|
||||
}
|
||||
21
core.worker/src/main/java/base/Receiver.java
Normal file
21
core.worker/src/main/java/base/Receiver.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base;
|
||||
|
||||
public interface Receiver {
|
||||
public void receive(byte[] buffer);
|
||||
}
|
||||
23
core.worker/src/main/java/base/Sender.java
Normal file
23
core.worker/src/main/java/base/Sender.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Sender extends Control {
|
||||
public void send(byte[] buffer) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception;
|
||||
|
||||
public class WorkerException extends Exception {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class ActivateException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class AlreadyActiveException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class AlreadyRunningException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class DeactivateException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class NotActiveException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class NotRunningException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception.worker;
|
||||
|
||||
import base.exception.WorkerException;
|
||||
|
||||
public class StartException extends WorkerException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
}
|
||||
56
core.worker/src/main/java/base/util/ArrayCycle.java
Normal file
56
core.worker/src/main/java/base/util/ArrayCycle.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.util;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class ArrayCycle<E> extends CopyOnWriteArrayList<E> {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
protected int index = 0;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ArrayCycle(E... elementArray) {
|
||||
if (elementArray != null) {
|
||||
for (E element : elementArray) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public E current() {
|
||||
return this.get(index);
|
||||
}
|
||||
|
||||
public synchronized E previous() {
|
||||
if (--index < 0) {
|
||||
index = Math.max(0, size() - 1);
|
||||
}
|
||||
return get(index);
|
||||
}
|
||||
|
||||
public synchronized E next() {
|
||||
if (++index >= size()) {
|
||||
index = 0;
|
||||
}
|
||||
return size() == 0 ? null : get(index);
|
||||
}
|
||||
|
||||
public E reset() {
|
||||
return get(index = 0);
|
||||
}
|
||||
}
|
||||
57
core.worker/src/main/java/base/util/Buffer.java
Normal file
57
core.worker/src/main/java/base/util/Buffer.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.util;
|
||||
|
||||
public class Buffer {
|
||||
protected byte[] elements;
|
||||
protected int capacity;
|
||||
protected int index;
|
||||
protected int size;
|
||||
|
||||
public Buffer(int capacity) {
|
||||
this.elements = new byte[capacity];
|
||||
this.capacity = capacity;
|
||||
index = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public synchronized void add(byte... elements) {
|
||||
for (byte element : elements) {
|
||||
this.elements[index++ % capacity] = element;
|
||||
if (size < capacity) {
|
||||
++size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void write(byte[] elements, int offset, int length) {
|
||||
for (int i = offset; i < length; ++i) {
|
||||
this.elements[index++ % capacity] = elements[i];
|
||||
if (size < capacity) {
|
||||
++size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized byte[] get() {
|
||||
byte[] elements = new byte[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = this.elements[(index + i) % size];
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
22
core.worker/src/main/java/base/util/Bufferable.java
Normal file
22
core.worker/src/main/java/base/util/Bufferable.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.util;
|
||||
|
||||
public interface Bufferable {
|
||||
public void load();
|
||||
public void unload();
|
||||
}
|
||||
90
core.worker/src/main/java/base/util/BufferedArrayCycle.java
Normal file
90
core.worker/src/main/java/base/util/BufferedArrayCycle.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.util;
|
||||
|
||||
public class BufferedArrayCycle<E extends Bufferable> extends ArrayCycle<E> {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
protected ArrayCycle<? extends Bufferable> buffer;
|
||||
protected int before;
|
||||
protected int after;
|
||||
protected int indexFirst;
|
||||
protected int indexLast;
|
||||
//protected int indexBuffer;
|
||||
//protected Bufferable[] bufferableArray;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BufferedArrayCycle(int before, int after) {
|
||||
this.before = before;
|
||||
this.after = after;
|
||||
indexFirst = 0;
|
||||
indexLast = 0;
|
||||
//bufferableArray = new Bufferable[before + after + 1];
|
||||
//buffer = new ArrayCycle<Bufferable>();
|
||||
|
||||
}
|
||||
|
||||
public E previous() {
|
||||
get(indexFirst).unload();
|
||||
indexFirst = previous(indexFirst);
|
||||
indexLast = previous(indexLast);
|
||||
get(indexLast).load();
|
||||
// eerste before weg
|
||||
|
||||
// eerste after wordt huidig
|
||||
|
||||
// voeg laatste after toe
|
||||
|
||||
return current();
|
||||
}
|
||||
|
||||
public E next() {
|
||||
|
||||
// eerste before weg
|
||||
|
||||
// eerste after wordt huidig
|
||||
|
||||
// voeg laatste after toe
|
||||
|
||||
return size() == 0 ? null : get(index);
|
||||
}
|
||||
|
||||
protected int previous(int index) {
|
||||
if (--index < 0) {
|
||||
index = Math.max(0, size() - 1);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
protected int next(int index) {
|
||||
System.out.println(index);
|
||||
if (++index >= size()) {
|
||||
index = 0;
|
||||
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BufferedArrayCycle<Dummy> bac = new BufferedArrayCycle<Dummy>(2, 3);
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
bac.add(new Dummy(i));
|
||||
}
|
||||
bac.remove(0);
|
||||
System.out.println(bac.get(2).id);
|
||||
}
|
||||
}
|
||||
34
core.worker/src/main/java/base/util/Dummy.java
Normal file
34
core.worker/src/main/java/base/util/Dummy.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.util;
|
||||
|
||||
public class Dummy implements Bufferable {
|
||||
public int id;
|
||||
|
||||
public Dummy(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
System.out.println("Dummy #" + id + ": load()");
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
System.out.println("Dummy #" + id + ": load()");
|
||||
}
|
||||
|
||||
}
|
||||
110
core.worker/src/main/java/base/work/Listen.java
Normal file
110
core.worker/src/main/java/base/work/Listen.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.work;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.worker.BackgroundListener;
|
||||
import base.worker.ForegroundListener;
|
||||
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 implements Listener<E> {
|
||||
protected static final Worker.Type WORKER_TYPE = Worker.Type.DIRECT;
|
||||
|
||||
protected Listener<E> listener;
|
||||
protected Worker.Type workerType;
|
||||
public Queue<E> queue;
|
||||
|
||||
public Listen() {
|
||||
this(WORKER_TYPE);
|
||||
}
|
||||
|
||||
protected Listen(Worker.Type workerType) {
|
||||
queue = new ConcurrentLinkedQueue<E>();
|
||||
this.workerType = workerType;
|
||||
switch (workerType) {
|
||||
case DIRECT:
|
||||
return;
|
||||
case FOREGROUND:
|
||||
listener = new ForegroundListener<E>(this);
|
||||
break;
|
||||
default:
|
||||
listener = new BackgroundListener<E>(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected Listen(Worker worker) {
|
||||
this.worker = 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) {
|
||||
if (workerType.equals(Worker.Type.DIRECT)) {
|
||||
input(element);
|
||||
} else {
|
||||
queue.add(element);
|
||||
listener.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (workerType.equals(Worker.Type.DIRECT)) {
|
||||
try {
|
||||
activate();
|
||||
} catch (ActivateException e) {
|
||||
logger.error("Failed to start directly", e);
|
||||
}
|
||||
} else {
|
||||
super.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
super.stop();
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
while (!queue.isEmpty()) {
|
||||
logger.debug("Listen: work() > input");
|
||||
input(queue.poll());
|
||||
}
|
||||
synchronized (this) {
|
||||
logger.debug("Listen: work() > wait");
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {}
|
||||
logger.debug("Listen: work() > notified");
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void input(E element);
|
||||
}
|
||||
48
core.worker/src/main/java/base/work/ReflectiveListen.java
Normal file
48
core.worker/src/main/java/base/work/ReflectiveListen.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.work;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
|
||||
import base.worker.Worker;
|
||||
|
||||
public class ReflectiveListen extends Listen<Object> {
|
||||
public ReflectiveListen() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ReflectiveListen(Worker.Type workerType) {
|
||||
super(workerType);
|
||||
}
|
||||
|
||||
public void input(Object object) {
|
||||
Class<?> clazz = object.getClass();
|
||||
MethodType methodType = MethodType.methodType(void.class, clazz);
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
MethodHandle methodHandle;
|
||||
try {
|
||||
methodHandle = lookup.findVirtual(getClass(), "input", methodType);
|
||||
methodHandle.invoke(this, object);
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
} catch (Throwable e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
89
core.worker/src/main/java/base/work/Work.java
Normal file
89
core.worker/src/main/java/base/work/Work.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.work;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.Control;
|
||||
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 implements Control {
|
||||
protected static final Worker.Type WORKER_TYPE = Worker.Type.BACKGROUND;
|
||||
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected Worker worker;
|
||||
|
||||
protected Work() {
|
||||
this(WORKER_TYPE);
|
||||
}
|
||||
|
||||
protected Work(Worker.Type workerType) {
|
||||
switch (workerType) {
|
||||
case FOREGROUND:
|
||||
worker = new DirectWorker(this);
|
||||
break;
|
||||
default:
|
||||
worker = new ThreadWorker(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected Work(Worker worker) {
|
||||
this.worker = worker;
|
||||
}
|
||||
|
||||
protected Work(WorkerPool workerPool) {
|
||||
worker = new PooledWorker(this);
|
||||
workerPool.add((PooledWorker) worker);
|
||||
}
|
||||
|
||||
protected void sleep(int time) {
|
||||
worker.sleep(time);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
logger.trace("Work: start()");
|
||||
worker.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
logger.trace("Work: stop()");
|
||||
worker.stop();
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
logger.trace("Work: active()");
|
||||
return worker.active();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
logger.debug("Work: exit()");
|
||||
worker.exit();
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {}
|
||||
public void deactivate() throws DeactivateException {}
|
||||
public abstract void work();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker;
|
||||
|
||||
import base.work.Listen;
|
||||
import base.worker.pool.Listener;
|
||||
|
||||
public class BackgroundListener<E> extends ThreadWorker implements Listener<E> {
|
||||
protected Listen<E> listen;
|
||||
|
||||
public BackgroundListener(Listen<E> listen) {
|
||||
this(listen, true);
|
||||
}
|
||||
|
||||
public BackgroundListener(Listen<E> listen, boolean thread) {
|
||||
super(listen, thread);
|
||||
this.listen = listen;
|
||||
}
|
||||
|
||||
public void add(E element) {
|
||||
listen.notify();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
25
core.worker/src/main/java/base/worker/DirectWorker.java
Normal file
25
core.worker/src/main/java/base/worker/DirectWorker.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker;
|
||||
|
||||
import base.work.Work;
|
||||
|
||||
public class DirectWorker extends ThreadWorker {
|
||||
public DirectWorker(Work work) {
|
||||
super(work, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker;
|
||||
|
||||
import base.work.Listen;
|
||||
import base.worker.pool.Listener;
|
||||
|
||||
public class ForegroundListener<E> extends BackgroundListener<E> implements Listener<E> {
|
||||
public ForegroundListener(Listen<E> listen) {
|
||||
super(listen, false);
|
||||
}
|
||||
}
|
||||
53
core.worker/src/main/java/base/worker/IntervalWork.java
Normal file
53
core.worker/src/main/java/base/worker/IntervalWork.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 FOREGROUND:
|
||||
worker = new DirectIntervalWorker(this);
|
||||
break;
|
||||
default:
|
||||
case BACKGROUND:
|
||||
worker = new ThreadIntervalWorker(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected IntervalWork(Worker.Type workerType, int interval) {
|
||||
switch (workerType) {
|
||||
case FOREGROUND:
|
||||
worker = new DirectIntervalWorker(this, interval);
|
||||
break;
|
||||
default:
|
||||
case BACKGROUND:
|
||||
worker = new ThreadIntervalWorker(this, interval);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
61
core.worker/src/main/java/base/worker/ThreadWorker.java
Normal file
61
core.worker/src/main/java/base/worker/ThreadWorker.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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, work.getClass().getName()).start();
|
||||
} else {
|
||||
logger.debug("Run directly");
|
||||
run();
|
||||
}
|
||||
} else {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
start(thread);
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
run = false;
|
||||
work.stop();
|
||||
}
|
||||
}
|
||||
130
core.worker/src/main/java/base/worker/Worker.java
Normal file
130
core.worker/src/main/java/base/worker/Worker.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class Worker {
|
||||
public enum Type {
|
||||
DIRECT, FOREGROUND, BACKGROUND, POOLED
|
||||
}
|
||||
|
||||
public static final int SLEEP = 100;
|
||||
|
||||
protected Logger logger;
|
||||
|
||||
protected boolean run = false;
|
||||
protected boolean active = false;
|
||||
protected boolean activate = false;
|
||||
protected boolean deactivate = false;
|
||||
|
||||
protected Work work;
|
||||
|
||||
public Worker(Work work) {
|
||||
this.work = work;
|
||||
logger = LoggerFactory.getLogger(work.getClass());
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
logger.trace("Worker: active()");
|
||||
return deactivate || active;
|
||||
}
|
||||
|
||||
public final void run() {
|
||||
logger.debug("Worker: run()");
|
||||
while (run || deactivate) {
|
||||
runActivate();
|
||||
runDeactivate();
|
||||
runWork();
|
||||
}
|
||||
}
|
||||
|
||||
public void runActivate() {
|
||||
if (activate && !active) {
|
||||
logger.trace("Worker: runActivate()");
|
||||
try {
|
||||
work.activate();
|
||||
active = true;
|
||||
} catch (ActivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
activate = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void runDeactivate() {
|
||||
if (deactivate && active) {
|
||||
logger.trace("Worker: runDeactivate()");
|
||||
try {
|
||||
work.deactivate();
|
||||
} catch (DeactivateException e) {
|
||||
logger.error("", e);
|
||||
} finally {
|
||||
deactivate = false;
|
||||
active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void runWork() {
|
||||
if (active) {
|
||||
logger.trace("Worker: runWork() > work");
|
||||
work.work();
|
||||
} else if (run) {
|
||||
try {
|
||||
logger.trace("Worker: runWork() > wait");
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void sleep() {
|
||||
sleep(SLEEP);
|
||||
}
|
||||
|
||||
public void sleep(int time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
Thread.sleep(time);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void start();
|
||||
|
||||
public void stop() {
|
||||
logger.trace("Worker: stop()");
|
||||
if (active && !activate) {
|
||||
deactivate = true;
|
||||
}
|
||||
activate = false;
|
||||
}
|
||||
|
||||
abstract public void exit();
|
||||
}
|
||||
21
core.worker/src/main/java/base/worker/pool/Listener.java
Normal file
21
core.worker/src/main/java/base/worker/pool/Listener.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
public interface Listener<E> {
|
||||
public void add(E element);
|
||||
}
|
||||
54
core.worker/src/main/java/base/worker/pool/ListenerPool.java
Normal file
54
core.worker/src/main/java/base/worker/pool/ListenerPool.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ListenerPool<E> {
|
||||
protected int poolSize;
|
||||
protected BlockingQueue<Wrapper<E>> queue;
|
||||
protected ExecutorService executorService;
|
||||
|
||||
public ListenerPool(int poolSize) {
|
||||
this.poolSize = poolSize;
|
||||
queue = new LinkedBlockingQueue<Wrapper<E>>();
|
||||
executorService = Executors.newFixedThreadPool(poolSize);
|
||||
}
|
||||
|
||||
public PooledListener<E> add(PooledListener<E> listener) {
|
||||
listener.setPoolQueue(queue);
|
||||
return listener;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
for (int i = 0; i < poolSize; ++i) {
|
||||
Runnable runnable = new ListenerPoolRunnable<E>(queue, i);
|
||||
executorService.execute(runnable);
|
||||
}
|
||||
}
|
||||
|
||||
public void await() {
|
||||
try {
|
||||
executorService.awaitTermination(0, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
class ListenerPoolRunnable<E> implements Runnable {
|
||||
protected BlockingQueue<Wrapper<E>> queue;
|
||||
protected int id;
|
||||
|
||||
public ListenerPoolRunnable(BlockingQueue<Wrapper<E>> queue, int id) {
|
||||
this.queue = queue;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
System.out.println("Thread #" + id + " waiting...");
|
||||
Wrapper<E> wrapper = queue.take();
|
||||
wrapper.deliver();
|
||||
Thread.sleep((int) (Math.random() * 1000));
|
||||
}
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
65
core.worker/src/main/java/base/worker/pool/PooledWorker.java
Normal file
65
core.worker/src/main/java/base/worker/pool/PooledWorker.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import base.work.Work;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class PooledWorker extends Worker {
|
||||
protected BlockingQueue<Worker> activateQueue;
|
||||
protected BlockingQueue<Worker> deactivateQueue;
|
||||
|
||||
public PooledWorker(Work work) {
|
||||
super(work);
|
||||
}
|
||||
|
||||
public void setActivateQueue(BlockingQueue<Worker> activateQueue) {
|
||||
this.activateQueue = activateQueue;
|
||||
}
|
||||
|
||||
public void setDeactivateQueue(BlockingQueue<Worker> deactivateQueue) {
|
||||
this.deactivateQueue = deactivateQueue;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (!active) {
|
||||
activate = true;
|
||||
}
|
||||
if (!run) {
|
||||
run = true;
|
||||
}
|
||||
try {
|
||||
deactivateQueue.remove(this);
|
||||
activateQueue.put(this);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("stop!! " + active);
|
||||
if (active) {
|
||||
deactivate = true;
|
||||
}
|
||||
activateQueue.remove(this);
|
||||
deactivateQueue.add(this);
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
58
core.worker/src/main/java/base/worker/pool/WorkerPool.java
Normal file
58
core.worker/src/main/java/base/worker/pool/WorkerPool.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import base.util.ArrayCycle;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class WorkerPool {
|
||||
protected int poolSize;
|
||||
protected BlockingQueue<Worker> activateQueue;
|
||||
protected BlockingQueue<Worker> deactivateQueue;
|
||||
protected ArrayCycle<Worker> workerCycle;
|
||||
protected ExecutorService executorService;
|
||||
|
||||
public WorkerPool(int poolSize) {
|
||||
this.poolSize = poolSize;
|
||||
activateQueue = new LinkedBlockingQueue<Worker>();
|
||||
deactivateQueue = new LinkedBlockingQueue<Worker>();
|
||||
workerCycle = new ArrayCycle<Worker>();
|
||||
executorService = Executors.newFixedThreadPool(poolSize);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
for (int i = 0; i < poolSize; ++i) {
|
||||
Runnable runnable = new WorkerPoolRunnable(activateQueue, deactivateQueue, workerCycle, i + 1);
|
||||
executorService.execute(runnable);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
// Must be graceful
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
|
||||
public void add(PooledWorker worker) {
|
||||
worker.setActivateQueue(activateQueue);
|
||||
worker.setDeactivateQueue(deactivateQueue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import base.util.ArrayCycle;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class WorkerPoolRunnable implements Runnable {
|
||||
protected BlockingQueue<Worker> activateQueue;
|
||||
protected BlockingQueue<Worker> deactivateQueue;
|
||||
protected ArrayCycle<Worker> workerCycle;
|
||||
protected int id;
|
||||
|
||||
public WorkerPoolRunnable(BlockingQueue<Worker> activateQueue, BlockingQueue<Worker> deactivateQueue, ArrayCycle<Worker> workerCycle, int id) {
|
||||
this.activateQueue = activateQueue;
|
||||
this.deactivateQueue = deactivateQueue;
|
||||
this.workerCycle = workerCycle;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
if (!deactivateQueue.isEmpty()) {
|
||||
try {
|
||||
Worker worker = deactivateQueue.take();
|
||||
worker.runDeactivate();
|
||||
workerCycle.remove(worker);
|
||||
} catch (InterruptedException e) {}
|
||||
} else if (!activateQueue.isEmpty() || workerCycle.isEmpty()) {
|
||||
try {
|
||||
Worker worker = activateQueue.take();
|
||||
worker.runActivate();
|
||||
workerCycle.add(worker);
|
||||
} catch (InterruptedException e) {}
|
||||
} else {
|
||||
Worker worker = workerCycle.next();
|
||||
worker.runWork();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
core.worker/src/main/java/base/worker/pool/Wrapper.java
Normal file
32
core.worker/src/main/java/base/worker/pool/Wrapper.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.worker.pool;
|
||||
|
||||
|
||||
class Wrapper<E> {
|
||||
protected PooledListener<E> listener;
|
||||
protected E element;
|
||||
|
||||
public Wrapper(PooledListener<E> listener, E element) {
|
||||
this.listener = listener;
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public void deliver() {
|
||||
listener.input(element);
|
||||
}
|
||||
}
|
||||
15
core.worker/src/test/java/worker/TestDirectWork.java
Normal file
15
core.worker/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
core.worker/src/test/java/worker/TestIntervalWork.java
Normal file
18
core.worker/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
core.worker/src/test/java/worker/TestListen.java
Normal file
19
core.worker/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<Integer> listen = new DummyListen<Integer>(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) {}
|
||||
}
|
||||
}
|
||||
38
core.worker/src/test/java/worker/TestPooledListen.java
Normal file
38
core.worker/src/test/java/worker/TestPooledListen.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package worker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import worker.dummy.DummyListen;
|
||||
import base.worker.pool.ListenerPool;
|
||||
|
||||
public class TestPooledListen {
|
||||
protected int id;
|
||||
|
||||
public TestPooledListen(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void input(Integer element) {
|
||||
System.out.println("#" + id + ": " + element);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListenerPool<Integer> listenerPool = new ListenerPool<Integer>(5);
|
||||
List<DummyListen<Integer>> listenList = new ArrayList<DummyListen<Integer>>();
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
DummyListen<Integer> listen = new DummyListen<Integer>(listenerPool, i + 1);
|
||||
listenList.add(listen);
|
||||
}
|
||||
listenerPool.start();
|
||||
|
||||
System.out.println("Starting to give out elements!");
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
DummyListen<Integer> randomListen = listenList.get((new Random()).nextInt(listenList.size()));
|
||||
randomListen.add(i);
|
||||
}
|
||||
|
||||
//listenerPool.await();
|
||||
}
|
||||
}
|
||||
45
core.worker/src/test/java/worker/TestPooledWork.java
Normal file
45
core.worker/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);
|
||||
}
|
||||
}
|
||||
@@ -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(":-)");
|
||||
}
|
||||
}
|
||||
32
core.worker/src/test/java/worker/dummy/DummyListen.java
Normal file
32
core.worker/src/test/java/worker/dummy/DummyListen.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package worker.dummy;
|
||||
|
||||
import base.work.Listen;
|
||||
import base.worker.pool.ListenerPool;
|
||||
|
||||
public class DummyListen<T> extends Listen<T> {
|
||||
protected int id;
|
||||
|
||||
public DummyListen(ListenerPool<T> 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);
|
||||
}
|
||||
|
||||
public void input(byte[] input) {
|
||||
System.out.println("#" + id + ", input = " + new String(input).trim());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void input(T element) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
42
core.worker/src/test/java/worker/dummy/DummyWork.java
Normal file
42
core.worker/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...");
|
||||
}
|
||||
}
|
||||
4
core.worker/src/test/resources/log4j.properties
Normal file
4
core.worker/src/test/resources/log4j.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
log4j.rootLogger=TRACE, CA
|
||||
log4j.appender.CA=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
|
||||
Reference in New Issue
Block a user