Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
d8d031f1
提交
d8d031f1
authored
16 年前
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New TaskProcess tool
上级
e2bac4d3
master
noel-pr1
plus33-master
pr/267
stumc-Issue#576
version-1.1.x
version-1.4.198
version-1.4.197
version-1.4.196
version-1.4.195
version-1.4.194
version-1.4.193
version-1.4.192
version-1.4.191
version-1.4.190
version-1.4.188
version-1.4.187
version-1.4.186
version-1.4.185
version-1.4.184
version-1.4.183
version-1.4.182
version-1.4.181
version-1.4.178
version-1.4.177
version-1.3
version-1.2
version-1.1
version-1.0
无相关合并请求
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
214 行增加
和
0 行删除
+214
-0
Task.java
h2/src/test/org/h2/test/db/Task.java
+77
-0
TaskProcess.java
h2/src/test/org/h2/test/db/TaskProcess.java
+137
-0
没有找到文件。
h2/src/test/org/h2/test/db/Task.java
0 → 100644
浏览文件 @
d8d031f1
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
db
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
org.h2.test.unit.SelfDestructor
;
/**
* A task that can be run as a separate process.
*/
public
abstract
class
Task
{
/**
* Run the class. This method is called by the task framework, and should
* not be called directly from the application.
*
* @param args the command line arguments
*/
public
static
void
main
(
String
[]
args
)
{
SelfDestructor
.
startCountdown
(
60
);
Task
task
;
try
{
String
className
=
args
[
0
];
task
=
(
Task
)
Class
.
forName
(
className
).
newInstance
();
System
.
out
.
println
(
"running"
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"init error: "
+
t
);
t
.
printStackTrace
();
return
;
}
try
{
String
[]
taskArgs
=
new
String
[
args
.
length
-
1
];
System
.
arraycopy
(
args
,
0
,
taskArgs
,
0
,
args
.
length
-
1
);
task
.
run
(
taskArgs
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"error: "
+
t
);
t
.
printStackTrace
();
}
}
/**
* Run the task.
*
* @param args the command line arguments
*/
abstract
void
run
(
String
[]
args
)
throws
Exception
;
/**
* Receive a message from the process over the standard output.
*
* @return the message
*/
protected
String
receive
()
{
try
{
return
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
)).
readLine
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Error reading from input"
,
e
);
}
}
/**
* Send a message to the process over the standard input.
*
* @param message the message
*/
protected
void
send
(
String
message
)
{
System
.
out
.
println
(
message
);
System
.
out
.
flush
();
}
}
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/db/TaskProcess.java
0 → 100644
浏览文件 @
d8d031f1
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
db
;
import
java.io.BufferedReader
;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
org.h2.test.unit.SelfDestructor
;
import
org.h2.util.StringUtils
;
/**
* A task that is run as an external process. This class communicates over
* standard input / output with the process. The standard error stream of the
* process is directly send to the standard error stream of this process.
*/
public
class
TaskProcess
{
private
final
Task
taskDef
;
private
Process
process
;
private
BufferedReader
reader
;
private
BufferedWriter
writer
;
/**
* Construct a new task process. The process is not started yet.
*
* @param taskDef the task
*/
public
TaskProcess
(
Task
taskDef
)
{
this
.
taskDef
=
taskDef
;
}
/**
* Start the task with the given arguments.
*
* @param args the arguments, or null
*/
public
void
start
(
String
[]
args
)
{
try
{
String
selfDestruct
=
SelfDestructor
.
getPropertyString
(
60
);
ArrayList
list
=
new
ArrayList
();
list
.
add
(
"java"
);
list
.
add
(
selfDestruct
);
list
.
add
(
"-cp"
);
list
.
add
(
"bin"
+
File
.
pathSeparator
+
"."
);
list
.
add
(
Task
.
class
.
getName
());
list
.
add
(
taskDef
.
getClass
().
getName
());
if
(
args
!=
null
&&
args
.
length
>
0
)
{
list
.
addAll
(
Arrays
.
asList
(
args
));
}
String
[]
procDef
=
new
String
[
list
.
size
()];
list
.
toArray
(
procDef
);
traceOperation
(
"start: "
+
StringUtils
.
arrayCombine
(
procDef
,
' '
));
process
=
Runtime
.
getRuntime
().
exec
(
procDef
);
copyInThread
(
process
.
getErrorStream
(),
System
.
err
);
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
writer
=
new
BufferedWriter
(
new
OutputStreamWriter
(
process
.
getOutputStream
()));
String
line
=
reader
.
readLine
();
if
(
line
==
null
)
{
throw
new
RuntimeException
(
"No reply from process, command: "
+
StringUtils
.
arrayCombine
(
procDef
,
' '
));
}
else
if
(
line
.
startsWith
(
"running"
))
{
traceOperation
(
"got reply: "
+
line
);
}
else
if
(
line
.
startsWith
(
"init error"
))
{
throw
new
RuntimeException
(
line
);
}
}
catch
(
Throwable
t
)
{
throw
new
RuntimeException
(
"Error starting task"
,
t
);
}
}
private
void
copyInThread
(
final
InputStream
in
,
final
OutputStream
out
)
{
new
Thread
()
{
public
void
run
()
{
try
{
while
(
true
)
{
int
x
=
in
.
read
();
if
(
x
<
0
)
{
return
;
}
if
(
out
!=
null
)
{
out
.
write
(
x
);
}
}
}
catch
(
Exception
e
)
{
throw
new
Error
(
"Error: "
+
e
,
e
);
}
}
}
.
start
();
}
/**
* Receive a message from the process over the standard output.
*
* @return the message
*/
public
String
receive
()
{
try
{
return
reader
.
readLine
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Error reading"
,
e
);
}
}
/**
* Send a message to the process over the standard input.
*
* @param message the message
*/
public
void
send
(
String
message
)
{
try
{
writer
.
write
(
message
+
"\n"
);
writer
.
flush
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Error writing "
+
message
,
e
);
}
}
/**
* Kill the process if it still runs.
*/
public
void
destroy
()
{
process
.
destroy
();
}
private
void
traceOperation
(
String
s
)
{
// ignore
}
}
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论