tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Gumbrecht <andy.gumbre...@orprovision.com>
Subject Re: Running standalone server as a windows service
Date Thu, 17 Dec 2009 08:30:31 GMT
It would actually be pretty easy to do - just running an embedded 
instance in the controller rather than spawning the standalone. I just 
decided to do it as-is after looking at the current way of spawning a 
server.
> I tried the Andy's suggestion and it works as expected.  It launches the
> openEJB in a separate runtime and communicates through the admin port to
> shutdown.
>
> I am wondering whether it would be a better idea if we tightly integrate
> openejb with the JSL service main class, without launching it in a separate
> process.  which means that openEJB main is called from the JSL service
> wrapper?.. I don't know the internals of the openEJB so I am not sure how
> big task this would be.
>
> Thanks
> /selvan
>
>
>
>
> rtselvan wrote:
>    
>> David, I haven't got around doing this yet, but I will have an update by
>> end of this week.
>>
>> /selvan
>>
>>
>> David Blevins wrote:
>>      
>>> Thanks, Andy, for the tip.  Selvan,  I'm curious how this worked out
>>> for you.
>>>
>>> I wonder if this is something we can incorporate into the build.
>>> Seems an out-of-the-box windows service version of OpenEJB standalone
>>> would be useful to a fair number of people.  Not being much of a
>>> windows guy I'm not too sure what it would take to make that a reality.
>>>
>>> The doors are open if anyone would like to take a whack at it.
>>>
>>> -David
>>>
>>> On Nov 23, 2009, at 7:28 PM, rtselvan wrote:
>>>
>>>        
>>>> Thanks Andy. This information is useful, I will try it out.
>>>>
>>>> Yeah, I know what you are talking about the Java Service Wrapper
>>>> project.
>>>>
>>>> Thanks
>>>>
>>>>
>>>>
>>>> Andy Gumbrecht wrote:
>>>>          
>>>>> Hi Selvan,
>>>>>
>>>>> The Java Service Wrapper is no longer free, so I went on the hunt -
>>>>> Its
>>>>> a shame really, because it was developed largely by the community.
>>>>>
>>>>> It is not rocket science to actually dig into the Java.c from Sun and
>>>>> get a basic service running yourself - It is a little more hard to
>>>>> cater
>>>>> for everyone though, and that is where the JSW finds it's commercial
>>>>> strength.
>>>>>
>>>>> Anyway, I used this project 'http://jslwin.sourceforge.net/' (Thanks
>>>>> Michael) to get OpenEJB running as a service using:
>>>>>
>>>>> 1. A simple controller...
>>>>>
>>>>> public class ServiceController {
>>>>>
>>>>>      private final static Log log =
>>>>> LogFactory.getLog(ServiceController.class);
>>>>>
>>>>>      private static Server _server;
>>>>>
>>>>>      public ServiceController() {
>>>>>      }
>>>>>
>>>>>      public static void main(String[] args) {
>>>>>
>>>>>          final File dir;
>>>>>
>>>>>          try {
>>>>>              dir = new
>>>>> File(System.getProperty("standalone.server.dir"));
>>>>>
>>>>>              if (!dir.exists()) {
>>>>>                  log.fatal(String.format("System property
>>>>> 'standalone.server.dir' is invalid: %s", dir));
>>>>>                  throw new RuntimeException("System property
>>>>> 'standalone.server.dir' is invalid");
>>>>>              }
>>>>>          } catch (Exception e) {
>>>>>              log.fatal("System property 'standalone.server.dir' not
>>>>> found");
>>>>>              throw new RuntimeException("System property
>>>>> 'standalone.server.dir' not found!");
>>>>>          }
>>>>>
>>>>>          log.info(String.format("Configuring server at: %s", dir));
>>>>>
>>>>>          _server = new ServerOpenEJB(dir.getAbsolutePath());
>>>>>
>>>>>          Thread t = new Thread() {
>>>>>              public void run() {
>>>>>                  ServiceController.start();
>>>>>
>>>>>                  while (true) { //System.exit
>>>>>                      try {
>>>>>                          Thread.sleep(Integer.MAX_VALUE);
>>>>>                      } catch (InterruptedException e) {
>>>>>                          //Ignore
>>>>>                          log.debug("Service was interrupted");
>>>>>                      }
>>>>>                  }
>>>>>              }
>>>>>          };
>>>>>
>>>>>          t.start();
>>>>>
>>>>>          try {
>>>>>              t.join();
>>>>>          } catch (InterruptedException e) {
>>>>>              log.debug("InterruptedException:", e);
>>>>>          }
>>>>>      }
>>>>>
>>>>>      public static void start() {
>>>>>          log.info("Starting service");
>>>>>          resume();
>>>>>          log.info("Started service");
>>>>>      }
>>>>>
>>>>>      public static void stop() {
>>>>>
>>>>>          log.info("Stopping service");
>>>>>          pause();
>>>>>          log.info("Stopped service");
>>>>>          LogFactory.releaseAll();
>>>>>
>>>>>          Thread.yield();
>>>>>
>>>>>          System.exit(0);
>>>>>      }
>>>>>
>>>>>      public static void pause() {
>>>>>          log.info("Pausing service");
>>>>>          if (null != _server) {
>>>>>              _server.stop();
>>>>>          }
>>>>>      }
>>>>>
>>>>>      public static void resume() {
>>>>>          if (null != _server) {
>>>>>              if (!_server.isStarted()) {
>>>>>                  log.info("Resuming service");
>>>>>                  _server.restart();
>>>>>              }
>>>>>          }
>>>>>      }
>>>>> }
>>>>>
>>>>> 2. A server wrapper (Modified the existing OpenEJB invoker code, so
>>>>> thank you OpenEJB team)
>>>>>
>>>>> public class ServerOpenEJB implements Server {
>>>>>
>>>>>      private static final Log log =
>>>>> LogFactory.getLog(ServerOpenEJB.class);
>>>>>      private final String _path;
>>>>>
>>>>>      public ServerOpenEJB(String path) {
>>>>>
>>>>>          if (!path.endsWith(File.separator)) {
>>>>>              path += File.separator;
>>>>>          }
>>>>>
>>>>>          _path = path;
>>>>>      }
>>>>>
>>>>>      @Override
>>>>>      public boolean isStarted() {
>>>>>          return connect();
>>>>>      }
>>>>>
>>>>>      @Override
>>>>>      public boolean restart() {
>>>>>          if (stop()) {
>>>>>              return start();
>>>>>          }
>>>>>
>>>>>          return false;
>>>>>      }
>>>>>
>>>>>      public boolean start() {
>>>>>
>>>>>          if (!connect()) {
>>>>>
>>>>>              forkServerProcess("start");
>>>>>              return connect(10, "localhost", 4201);
>>>>>
>>>>>          } else {
>>>>>              log.debug(":: Remote OpenEJB already started ::");
>>>>>              return true;
>>>>>          }
>>>>>      }
>>>>>
>>>>>      @Override
>>>>>      public boolean stop() {
>>>>>
>>>>>          if (connect()) {
>>>>>
>>>>>              OutputStream out = null;
>>>>>              Socket socket = null;
>>>>>
>>>>>              try {
>>>>>                  socket = new Socket("localhost", 4200);
>>>>>                  out = socket.getOutputStream();
>>>>>                  out.write("Stop".getBytes());
>>>>>              } catch (Exception e) {
>>>>>                  //Ignore
>>>>>              } finally {
>>>>>                  try {
>>>>>                      out.close();
>>>>>                  } catch (IOException e) {
>>>>>                      //Ignore
>>>>>                  }
>>>>>                  try {
>>>>>                      socket.close();
>>>>>                  } catch (IOException e) {
>>>>>                      //Ignore
>>>>>                  }
>>>>>              }
>>>>>
>>>>>              return disconnect(10, "localhost", 4201);
>>>>>
>>>>>          } else {
>>>>>              log.debug(":: Remote OpenEJB is not running ::");
>>>>>              return true;
>>>>>          }
>>>>>      }
>>>>>
>>>>>      public static boolean connect() {
>>>>>          return connect(1, "localhost", 4201);
>>>>>      }
>>>>>
>>>>>      private void forkServerProcess(final String option) {
>>>>>
>>>>>          try {
>>>>>
>>>>>              ArrayList<String>  cmd = new ArrayList<String>();
>>>>>              String s = java.io.File.separator;
>>>>>              String java = System.getProperty("java.home") + s +
>>>>> "bin" +
>>>>> s + "java";
>>>>>
>>>>>              cmd.add(java);
>>>>>              addSystemProperties(cmd);
>>>>>              cmd.add("-classpath");
>>>>>              //cmd.add(getClasspath());
>>>>>              cmd.add(_path + "lib");
>>>>>              //cmd.add("org.apache.openejb.server.Main");
>>>>>              cmd.add("-jar");
>>>>>
>>>>>              //Find the core jar.
>>>>>              final File[] libs = new File(_path + "lib").listFiles();
>>>>>              File core = null;
>>>>>
>>>>>              for(File f : libs){
>>>>>                  if(f.getName().toLowerCase().indexOf("openejb-
>>>>> core")>
>>>>> -1){
>>>>>                      core = f;
>>>>>                  }
>>>>>              }
>>>>>
>>>>>              if(null == core){
>>>>>                  throw new Exception("Did not find core library");
>>>>>              }
>>>>>
>>>>>              cmd.add(core.getAbsolutePath());
>>>>>              cmd.add(option);
>>>>>
>>>>>              String[] command = (String[]) cmd.toArray(new String[0]);
>>>>>
>>>>>              Runtime runtime = Runtime.getRuntime();
>>>>>              Process server = runtime.exec(command, null, new
>>>>> File(_path));
>>>>>
>>>>>              Thread serverOut = new Thread(new
>>>>> Pipe(server.getInputStream(), System.out));
>>>>>              serverOut.setDaemon(true);
>>>>>              serverOut.start();
>>>>>
>>>>>              Thread serverErr = new Thread(new
>>>>> Pipe(server.getErrorStream(), System.err));
>>>>>              serverErr.setDaemon(true);
>>>>>              serverErr.start();
>>>>>
>>>>>          } catch (Exception e) {
>>>>>              throw new RuntimeException("Cannot start the OpenEJB
>>>>> server.",e);
>>>>>          }
>>>>>
>>>>>      }
>>>>>
>>>>>      private void addSystemProperties(ArrayList<String>  cmd) {
>>>>>
>>>>>          Set set = System.getProperties().entrySet();
>>>>>
>>>>>          for (Iterator iter = set.iterator(); iter.hasNext();) {
>>>>>
>>>>>              Map.Entry entry = (Map.Entry) iter.next();
>>>>>              String key = (String) entry.getKey();
>>>>>              String value = (String) entry.getValue();
>>>>>
>>>>>              if (key.matches("^-X.*")) {
>>>>>                  cmd.add(key + value);
>>>>>              } else if
>>>>> (!key.matches("^(java|javax|os|sun|user|file|awt|line|path)\\..*")) {
>>>>>                  cmd.add("-D" + key + "=" + value);
>>>>>              }
>>>>>          }
>>>>>
>>>>>      }
>>>>>
>>>>>      public static boolean connect(int tries, String host, int port)
{
>>>>>
>>>>>          Socket s = null;
>>>>>
>>>>>          try {
>>>>>
>>>>>              s = new Socket(host, port);
>>>>>              s.getOutputStream().close();
>>>>>
>>>>>          } catch (Exception e) {
>>>>>
>>>>>              if (tries<  2) {
>>>>>                  return false;
>>>>>              } else {
>>>>>
>>>>>                  try {
>>>>>                      Thread.sleep(2000);
>>>>>                  } catch (Exception e2) {
>>>>>                      e.printStackTrace();
>>>>>                  }
>>>>>
>>>>>                  return connect(--tries, host, port);
>>>>>              }
>>>>>          } finally {
>>>>>              try {
>>>>>                  s.close();
>>>>>              } catch (Exception e) {
>>>>>              }
>>>>>          }
>>>>>
>>>>>          return true;
>>>>>      }
>>>>>
>>>>>      public static boolean disconnect(int tries, String host, int
>>>>> port) {
>>>>>
>>>>>          Socket s = null;
>>>>>
>>>>>          try {
>>>>>
>>>>>              s = new Socket(host, port);
>>>>>              s.getOutputStream().close();
>>>>>
>>>>>              try {
>>>>>                  s.close();
>>>>>              } catch (Exception e) {
>>>>>              }
>>>>>
>>>>>              if (tries<  2) {
>>>>>                  return false;
>>>>>              } else {
>>>>>
>>>>>                  try {
>>>>>                      Thread.sleep(2000);
>>>>>                  } catch (Exception e2) {
>>>>>                  }
>>>>>
>>>>>                  return disconnect(--tries, host, port);
>>>>>              }
>>>>>
>>>>>          } catch (Exception e) {
>>>>>
>>>>>          } finally {
>>>>>              try {
>>>>>                  s.close();
>>>>>              } catch (Exception e) {
>>>>>              }
>>>>>          }
>>>>>
>>>>>          return true;
>>>>>      }
>>>>>
>>>>>      private static final class Pipe implements Runnable {
>>>>>
>>>>>          private final InputStream is;
>>>>>          private final OutputStream out;
>>>>>
>>>>>          private Pipe(InputStream is, OutputStream out) {
>>>>>              super();
>>>>>              this.is = is;
>>>>>              this.out = out;
>>>>>          }
>>>>>
>>>>>          public void run() {
>>>>>
>>>>>              try {
>>>>>
>>>>>                  int i = is.read();
>>>>>                  out.write(i);
>>>>>
>>>>>                  while (i != -1) {
>>>>>                      i = is.read();
>>>>>                      out.write(i);
>>>>>                  }
>>>>>
>>>>>              } catch (Exception e) {
>>>>>                  e.printStackTrace();
>>>>>              }
>>>>>
>>>>>          }
>>>>>
>>>>>      }
>>>>> }
>>>>>
>>>>> The JSL service configuration is well documented and I am sure the
>>>>> above
>>>>> code can be tweaked to your needs.
>>>>>
>>>>> Best regards,
>>>>>
>>>>> Andy.
>>>>>
>>>>> On 23.11.2009 03:04, rtselvan wrote:
>>>>>            
>>>>>> It could be that I missed the documentation on it.
>>>>>>
>>>>>> Is there a wrapper integration to run openEJB as standalone windows
>>>>>> service?
>>>>>> openejb.bat doesn't have any option to install the open ejb as
>>>>>> windows
>>>>>> service (like tomcat).
>>>>>>
>>>>>> Thanks
>>>>>> /selvan
>>>>>>
>>>>>>              
>>>>>
>>>>>            
>>>> -- 
>>>> View this message in context:
>>>> http://old.nabble.com/Running-standalone-server-as-a-windows-service-tp26471759p26483456.html
>>>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>>>
>>>>
>>>>          
>>>
>>>
>>>        
>>
>>      
>    


-- 
------------------------------------------------------------------------

*Andy Gumbrecht*
Software Developer
Orpro Vision GmbH
Hefehof 8, 31785, Hameln

Tel +49 (0) 5151 809 44 21
Cell +49 (0) 174 1800 381
Email andy.gumbrecht@orprovision.com
Web www.orprovision.com


            Orpro Vision GmbH
            Sitz der Gesellschaft: 31785, Hameln
            USt-Id-Nr: DE264453214
            Amtsgericht Hannover HRB204336
            Geschaeftsfuehrer: Roberto Gatti

------------------------------------------------------------------------


            Diese E-Mail enthält vertrauliche und/oder rechtlich
            geschützte Informationen. Wenn Sie nicht der richtige
            Adressat sind oder diese E-Mail irrtümlich erhalten haben,
            informieren Sie bitte sofort den Absender und vernichten Sie
            diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
            Verwendung sowie die unbefugte Weitergabe dieser Mail ist
            nicht gestattet.

------------------------------------------------------------------------


            This e-mail may contain confidential and/or privileged
            information. If you are not the intended recipient (or have
            received this e-mail in error) please notify the sender
            immediately and destroy this e-mail. Any unauthorized
            copying, disclosure, distribution or other use of the
            material or parts thereof is strictly forbidden.

------------------------------------------------------------------------



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message