Converted some files back to java, made sure they work properly with java11 single file execution

This commit is contained in:
Robinson 2022-01-20 00:03:28 +01:00
parent 29c81785de
commit f6515341a5
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
4 changed files with 108 additions and 77 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 dorkbox, llc
* Copyright 2022 dorkbox, llc
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
* Contains fragments of code from Apache Commons Exec, rights owned
* by Apache Software Foundation (ASF).
@ -20,8 +20,9 @@
package dorkbox.executor.shutdown
import dorkbox.executor.Executor
import org.junit.Assert
import org.junit.Test
import dorkbox.executor.samples.TestSetup
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.io.File
/**
@ -43,14 +44,18 @@ class ProcessExecutorShutdownHookTest {
@Throws(Exception::class)
private fun testDestroyOnExit(loopStarterClassFile: Class<*>, fileIsAlwaysCreated: Boolean) {
val file = WriterLoop.getFile()
println(file)
println("Data loop file: $file")
if (file.exists()) file.delete()
Executor()
.redirectOutputAsInfo()
.redirectErrorAsInfo()
.enableRead()
.defaultLogger()
.asJvmProcess()
.cloneClasspath()
.setMainClass(loopStarterClassFile.name)
.setMainClass(TestSetup.getFile(loopStarterClassFile))
.startBlocking()
// After WriterLoopStarter has finished we expect that WriterLoop is also finished - no-one is updating the file
@ -61,15 +66,15 @@ class ProcessExecutorShutdownHookTest {
}
companion object {
private const val SLEEP_FOR_RECHECKING_FILE: Long = 4000
private const val SLEEP_FOR_RECHECKING_FILE: Long = 4000L
@Throws(InterruptedException::class)
private fun checkFileStaysTheSame(file: File) {
Assert.assertTrue(file.exists())
Assertions.assertTrue(file.exists())
val length = file.length()
Thread.sleep(SLEEP_FOR_RECHECKING_FILE)
Assert.assertEquals("File '$file' was still updated.", length, file.length())
Assertions.assertEquals(length, file.length(), "File '$file' was still updated.",)
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020 dorkbox, llc
* Copyright 2022 dorkbox, llc
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
* Contains fragments of code from Apache Commons Exec, rights owned
* by Apache Software Foundation (ASF).
@ -17,37 +17,40 @@
* limitations under the License.
*/
package dorkbox.executor.shutdown
import dorkbox.executor.Executor
import dorkbox.executor.samples.TestSetup
import kotlin.system.exitProcess
package dorkbox.executor.shutdown;
import dorkbox.executor.Executor;
import dorkbox.executor.samples.TestSetup;
/**
* Starts [WriterLoop] inside shutdown hook and destroys it.
*/
class WriterLoopStarterAfterExit : Runnable {
companion object {
private const val SLEEP_AFTER_START: Long = 2000
public
class WriterLoopStarterAfterExit implements Runnable {
private static Long SLEEP_AFTER_START = 2000L;
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
Runtime.getRuntime().addShutdownHook(Thread(WriterLoopStarterAfterExit()))
// Launch the process and also destroy it
exitProcess(0)
}
public static
void main(String[] args) {
System.out.println("Starting output: " + WriterLoop.getFile());
Runtime.getRuntime().addShutdownHook(new Thread(new WriterLoopStarterAfterExit()));
// Launch the process and also destroy it
System.exit(0);
}
override fun run() {
@Override
public
void run() {
try {
Executor("java", TestSetup.getFile(WriterLoop::class.java))
.destroyOnExit().startBlocking()
Thread.sleep(SLEEP_AFTER_START)
} catch (e: Exception) {
e.printStackTrace()
new Executor("java", TestSetup.INSTANCE.getFile(WriterLoop.class))
.destroyOnExit()
.startBlocking();
Thread.sleep(SLEEP_AFTER_START);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2022 dorkbox, llc
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
* Contains fragments of code from Apache Commons Exec, rights owned
* by Apache Software Foundation (ASF).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.executor.shutdown;
import java.io.File;
import java.io.IOException;
import dorkbox.executor.Executor;
/**
* Starts [WriterLoop] and destroys it on JVM exit.
*/
public
class WriterLoopStarterBeforeExit {
public static
void main(String[] args) {
try {
System.out.println("Starting output: " + new File("writeLoop.data").getAbsoluteFile());
// silly workarounds, because executing java files from CLI, require SINGLE-FILE access!
String path = getFile(WriterLoopStarterBeforeExit.class)
.replace(WriterLoopStarterBeforeExit.class.getSimpleName(), "WriterLoop");
System.out.println("Starting output: " + path);
new Executor()
.redirectOutputAsInfo()
.redirectErrorAsInfo()
.asJvmProcess()
.cloneClasspath()
.setMainClass(path)
.startBlocking();
} catch (Exception e) {
e.printStackTrace();
}
// Launch the process and also destroy it
System.exit(0);
}
private static String getFile(Class javaClass) throws IOException {
String properName;
if (Executor.Companion.getIS_OS_WINDOWS()) {
properName = javaClass.getName().replace('.', '\\');
} else {
properName = javaClass.getName().replace('.', '/');
}
File file = new File("test", properName + ".java").getAbsoluteFile().getCanonicalFile();
return file.getPath();
}
}

View File

@ -1,47 +0,0 @@
/*
* Copyright 2020 dorkbox, llc
* Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
* Contains fragments of code from Apache Commons Exec, rights owned
* by Apache Software Foundation (ASF).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.executor.shutdown
import dorkbox.executor.Executor
import dorkbox.executor.samples.TestSetup
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.system.exitProcess
/**
* Starts [WriterLoop] and destroys it on JVM exit.
*/
object WriterLoopStarterBeforeExit {
private const val SLEEP_AFTER_START: Long = 2000
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
Executor("java", TestSetup.getFile(WriterLoop::class.java))
.destroyOnExit().redirectOutputAsInfo().startAsync()
runBlocking {
delay(SLEEP_AFTER_START)
}
// Cause the launched process to be destroyed
exitProcess(0)
}
}