Runtime.exec freezes when calling an ant script that contains an hbm2ddl task?

To run the ant script, I use the exec method for the java.lang.Runtime class as follows:

Process process = Runtime.getRuntime (). Exec (JAVA_HOME ANT_HOME -jar / lib / ant -launcher.jar-BuildFile file.xml);

This method, despite its apparent simplicity, but it creates several problems and is described in javadoc as follows:

Because some proprietary platforms provide a limited buffer size for standard input and output streams, the inability to quickly write input to a stream or read the output of a subprocess. May cause blocking subprocess and even deadlock.

To solve this problem, I mentioned the following article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1 This method worked on non-machine (64-bit Windows 7 , Core 2 Quad Q9400 @ 2.66 GHz 2.67 GHz, 4 GB) But when I used another machine (XP SP3, Core 2 Duo 2.99 GHz at 3 GHz, 3.21 GB of memory), the process freezes when creating data schemes, and the console remains in desperate silence after reporting the following message:

[hibernatetool] Executing the Hibernate tool with the JPA setting Warning: the link is not compilation.classpath.id HAS was set to runtime, the target of WAS DURING was found parsing the assembly file, attempting to resolve. Future versions of ant May contain identifier references Defined for unfulfilled purposes.

[Hibernatetool] 1. Task: hbm2ddl (Creates a database schema).

Have you encountered such a problem? Do you have a solution for me? I am open to any decision. Note: my ant script looks like this:

 <project> .. <target name="create-jpa-schema"> <tstamp> <format property="timestamp.for.sql" pattern="yyyy-MM-dd_HH-mm-ss" /> </ Tstamp> <hibernatetool destdir="${build.sql.dir}"> <classpath> <path refid="classpath.id" /> <pathelement path="${model.jar}" /> </ Classpath> <jpaconfiguration persistenceunit="studio-pu" /> <! - Export schema to SQL database and run it Against -> <Hbm2ddl drop = "false" update = "true" create = "true" export = "true" outputfilename = "$ {schema_ timestamp.for.sql}. Sql" delimiter = "" format = "true" haltOnError = "true "/> </ Hibernatetool> </ Target> .. </ Project> 

EDIT: Code executing runtime.exec:

 public static int executeCommand( String cmd, File directory) throws IOException, InterruptedException { Process process = Runtime.getRuntime().exec(cmd, null, directory); StreamReader outputStreamReader = new StreamReader( process.getInputStream(), "OUTPUT"); StreamReader errorStreamReader = new StreamReader(process.getErrorStream(), "ERROR"); final Thread outputThreadReader = new Thread(outputStreamReader); final Thread errorThreadReader = new Thread(errorStreamReader); outputThreadReader.start(); errorThreadReader.start(); int exitCode = process.waitFor(); System.out.println("exit code:" + exitCode); return exitCode; } 

StreamReader.java

 public class StreamReader implements Runnable { InputStream is; OutputStream os; String type; StreamReader(InputStream is, String type) { this.is = is; this.type = type; } StreamReader(InputStream is, OutputStream os, String type) { this.is = is; this.os = os; this.type = type; } @Override public void run() { try { PrintWriter pw = null; if (os != null) pw = new PrintWriter(os); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (pw != null) { pw.println(line); pw.flush(); } else System.out.println(type + ">" + line); if(progressListener!=null) progressListener.onUpdate(line); } } catch (IOException ioe) { ioe.printStackTrace(); } } } 
+6
source share
3 answers

I'm not sure if this is possible, but did you think that calling ant programmatically instead of starting a new process?

According to this article, you should be able to do the following in a new thread, instead of exposing the process:

 File buildFile = new File("build.xml"); Project p = new Project(); p.setUserProperty("ant.file", buildFile.getAbsolutePath()); p.init(); ProjectHelper helper = ProjectHelper.getProjectHelper(); p.addReference("ant.projectHelper", helper); helper.parse(p, buildFile); p.executeTarget(p.getDefaultTarget()); 
+1
source

Add System.out.println() before and after readLine() in StreamReader. Find out if it continues to read actively when the console freezes, or that the stream exited prematurely.

+1
source

Is the circuit you export particularly large? Will it produce a lot of results?

In ant task hbm2ddl there is an attribute "console", which, if console="true" , copies ddl to System.out.println() , which can cause problems. This could ruin the buffering on your XP server.

The default is console = "true". Try console = "false".

 <Hbm2ddl console="false" drop = "false" update = "true" create = "true" export = "true" outputfilename = "$ {schema_ timestamp.for.sql}. Sql" delimiter = "" format = "true" haltOnError = "true "/> 

For more information, see the Hbm2ddl ant task code.

+1
source

Source: https://habr.com/ru/post/895168/


All Articles