0
votes

I have written a Java code that must run only after successful shutting down of weblogic server.

The code shuts down the weblogic server and performs the required operations and then restarts the servers.

My question now is .. Is there a way by which 1 can keep a tab on the weblogic console to see if the shutdown proceess is over or not or if there were some exceptions thrown by the server during startup or shut down(not manually but programmatically)??

In my local machine I had made the thread to sleep until the server was shutdown completely by keeping the average time to shut (manually).

The code goes as under:

static final String DOMAIN_NAMES = "domainNames";
static final String DOMAIN_HOME="domainHome";
public static void main(String[] argss) 
{
    AbstractApplicationContext context=new FileSystemXmlApplicationContext(new String[]{"src/main/resources/redeployconfig.xml"});
    StepOneRedeploy stepOne=(StepOneRedeploy) context.getBean("stepOneRedeploy");
    stepOne.setProperties();
    Properties prop=stepOne.getProperties();
    String domainNames = prop.getProperty(DOMAIN_NAMES);
    System.out.println(domainNames);
    String domainHome = prop.getProperty(DOMAIN_HOME);
    StringTokenizer domainNamesTokens= new StringTokenizer(domainNames,",");
    StringTokenizer domainNamesTokens2=domainNamesTokens;

    ArrayList<String> serverIPListArray = new ArrayList<String>();
    while (domainNamesTokens.hasMoreElements()) 
    {
        String domainName=domainNamesTokens.nextToken();
        System.out.println(domainName);
        serverIPListArray.add(domainName);
    }

    while(domainNamesTokens2.hasMoreElements())
    {
        String domainName=domainNamesTokens2.nextToken();


         /** Stopping weblogicserver **/
        String domainPathToShutServer = domainHome + domainName+"/stopWebLogic.cmd";
        String commandToShutServer="cmd /C start "+domainPathToShutServer;

        try 
        {
            Process p = Runtime.getRuntime().exec(commandToShutServer,null);
        } 

        catch (Exception e) 
        {
            e.printStackTrace();
        }


        /**Putting thread to sleep for 1 minute**/
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        /** Deleting temp folder **/
        try
        {
        File delTmpFile=new File("");
        boolean isTmpDelete=delTmpFile.delete();
        System.out.println(isTmpDelete);
        if(!isTmpDelete)
        {
            throw new TempDeleteFailedException("Could not delete Tmp folder for "+domainName);
        }
        }
        catch(TempDeleteFailedException tdfe)
        {
            tdfe.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        /** Deleting stage folder **/
        try 
        {
            File delStgFile=new File("");
            boolean isStgDelete=delStgFile.delete();
            System.out.println(isStgDelete);
            if(isStgDelete)
            {
                throw new StageDeleteFailedException();
            }
        } 
        catch (StageDeleteFailedException stgDelEileException) 
        {
            stgDelEileException.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }





        /** Starting weblogicserver **/
        String domainPath = domainHome + domainName+"/startWebLogic.cmd";
        String command="cmd /C start "+domainPath;

        try 
        {
            Process p = Runtime.getRuntime().exec(command,null);
        } 

        catch (Exception e) 
        {
            e.printStackTrace();
        }



    }

}

I have tried ProcessBuilder to start weblogic server. But it says that

java.io.IOException: Cannot run program "startWebLogic.cmd" (in directory "D:\Oracle\Middleware\user_projects\domains\ass1"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
at TestMain.main(TestMain.java:35)

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:177) at java.lang.ProcessImpl.start(ProcessImpl.java:28) at java.lang.ProcessBuilder.start(ProcessBuilder.java:452) ... 1 more So if I were to refactor the Question...

How can 1 catch the exceptions thrown by the executable run using Java code?

2
Will using ProcessBuilder give me a hook to catch the exceptions thrown by the .cmd file(startWebLogic.cmd/stopWebLogic.cmd)? - JMD
How can 1 catch the exceptions thrown by the executable run using Java code? - JMD

2 Answers

1
votes

"Waiting enough" is a bad idea as it will easily break when the situation changes, and using a process builder is very limiting (not to mention that, these startup scripts typically spawn the main process as a separate process so you don't get to watch them to start with).

I'd look into hooks that the WebLogic server provides. WebLogic has lots of hooks so I don't know which one would be appropriate for you, but for example if it's a servlet you could do your shutdown process from a ServletContextListener.

1
votes

Is there a way by which 1 can keep a tab on the weblogic console to see if the shutdown proceess is over or not or if there were some exceptions thrown by the server during startup or shut down(not manually but programmatically)??

Weblogic logs are written to disk. By default they ae in DOMAIN_NAME\servers\SERVER_NAME\logs\SERVER_NAME.log(see: Understanding WebLogic logging files).

Just monitor the server log for exceptions. Take a look at Commons IO - Tailer.

By the way, you can use WLST and JMX to stop, start, restart and monitor the health of Weblogic (see ServerRuntimeMBean and ServerLifeCycleRuntimeMBean), no need to fire standalone processes.

You can either: