Creating a java Services Interface launcher


Author:

Fabio Andrés Palmieri
Green Developer.

Abstract:

This application is developed in Visual Studio 2010, and it is able to start a java Class as a Windows Services.

Contents:


Binaries / Full Sources
(*password is requiered contact to author)

Features:

  • Autostartable java class.
  • Error logs.
  • Easy to install, copy & run...
  • Java Start Event forces to Start java Class.
  • Java Stop Event forces to close java Class Started.
  • Java Class Stop Event, refresh the stop/start windows services status.
  • .net4.xx Required, (Optimized to parallel proccess)

Requiered:


Imports System
Imports System.Diagnostics.Process
Imports System.Threading
 
 
Public Class EdgeSystemLauncher
 
    Dim I As Integer
 
    Private arg() As String
 
    Private myJobName As String
 
    Private myJobId As Integer
 
    Private miProc As System.Diagnostics.Process
 
    Private out As System.IO.StreamWriter
 
    Private fn As String
 
    Private iterator As Integer
 
    Private t As Thread
 
    Protected Overrides Sub OnStart(ByVal args() As String)
        Try
            ChDir(Me.arg(0))
            fn = Me.arg(0) + "stdout.log"
            out = My.Computer.FileSystem.OpenTextFileWriter(fn, True)
            out.WriteLine(Date.Now().ToString() + ":" + "Starting Java Services Interface - details ")
            out.WriteLine(Date.Now().ToString() + ":" + "path=" + Me.arg(0))
            out.WriteLine(Date.Now().ToString() + ":" + "java-classname=" + Me.arg(1))
            I = Shell("java " + Me.arg(1), AppWinStyle.NormalFocus, False)
            out.WriteLine(Date.Now().ToString() + ":Started.")
            For Each p In System.Diagnostics.Process.GetProcesses()
                If (p.Id = I) Then
                    myJobName = p.ProcessName
                    myJobId = p.Id
                    miProc = p
                    out.WriteLine(Date.Now().ToString() + ":" + myJobName + " is running as #" + myJobId.ToString())
                Else
                End If
            Next
            out.Flush()
            out.Close()
 
            t = New Thread(AddressOf Status)
            t.Start()
 
        Catch ex As Exception
            out.WriteLine(Date.Now().ToString() + ":" + ex.ToString())
            out.Flush()
            out.Close()
        Finally
 
        End Try
    End Sub
 
    Protected Sub Status()
        Dim haga As Boolean
        haga = True
        While (haga)
            Thread.Sleep(5000)
            If (miProc.HasExited = True) Then
                Try
                    fn = Me.arg(0) + "stdout.log"
                    out = My.Computer.FileSystem.OpenTextFileWriter(fn, True)
                    out.WriteLine(Date.Now().ToString() + ":JSI exited unexpectedly")
                    out.Flush()
                    out.Close()
                Catch ex As Exception
                    out.WriteLine(Date.Now().ToString() + ":Error, JSI thrown an internal exception:" + ex.ToString())
                    out.Flush()
                    out.Close()
                End Try
                haga = False
                ExitCode = -1
                End
            Else
                'nothing
            End If
        End While
    End Sub
 
    Protected Overrides Sub OnStop()
        Try
            fn = Me.arg(0) + "stdout.log"
            out = My.Computer.FileSystem.OpenTextFileWriter(fn, True)
            out.WriteLine(Date.Now().ToString() + ":Shutdowing JSI Services")
            miProc.Kill()
            out.WriteLine(Date.Now().ToString() + ":Stop Event on JSI, ok")
            out.Flush()
            out.Close()
        Catch ex As Exception
            out.WriteLine(Date.Now().ToString() + ":Error, JSI wasn't running when JSI was stopped:" + ex.ToString())
            out.Flush()
            out.Close()
        End Try
 
    End Sub
 
    Private app() As String
 
    Public Sub New(ByVal args() As String)
        app = args
        ' This call is required by the designer.
        InitializeComponent(args)
 
        ' Add any initialization after the InitializeComponent() call.
 
    End Sub
End Class
 

Explanation:

1. Start a java Class as Windows Application.
I = Shell("java " + Me.arg(1), AppWinStyle.NormalFocus, False)

Me.arg(0) contains the java Class Name, this argument is passed as value when JSI.exe is started as JSI $path $javaClass $procName


2. Create a proccess id references to started java class
            For Each p In System.Diagnostics.Process.GetProcesses()
                If (p.Id = I) Then
                    myJobName = p.ProcessName
                    myJobId = p.Id
                    miProc = p
                    out.WriteLine(Date.Now().ToString() + ":" + myJobName + " is running as #" + myJobId.ToString())
                Else
                End If
            Next
 
 
Copyrigth (C) 2010 TALF Colombia


Fabio Andrés Palmiery Villa.
Green Developer.