devnumbertwo - $h!t for developers

View Original

create windows service weblogic AdminServer

installation notes and $h!t on how to create windows service weblogic admin server…

this has been verified on weblogic 10.3.6 but i believe it will work for any 10.3.x version of weblogic.

 *** Obviously, CHANGE ANY VALUES IN BLUE TO MATCH YOUR OWN VALUES

  1. Create a folder to keep all your windows service related files in, i created a winservices folder in my domain

    C:\oracle\user_projects\domains\devtwo_domain\winservices
  2. Create a cmd file named createAdminWinService.cmd in the winservices folder with the following contents

    echo off
    SETLOCAL
    set DOMAIN_NAME=devtwo_domain
    set USERDOMAIN_HOME=c:\oracle\user_projects\domains\devtwo_domain
    set SERVER_NAME=AdminServer
    call "c:\oracle\wlserver_10.3\server\bin\installSvc.cmd"
    ENDLOCAL
    
  3. Go to the WL_HOME\server\bin directory – in my case it’s c:\oracle\wlserver_10.3\server\bin
    Backup installSvc.cmd
    Then edit installSvc.cmd to contain the following changes

    ...
    set WL_HOME=C:\app\oracle\product\fmw\wlserver_10.3
    @rem call "%WL_HOME%\common\bin\commEnv.cmd"
    call %USERDOMAIN_HOME%\bin\setDomainEnv.cmd
    ...
    set CLASSPATH=%WEBLOGIC_CLASSPATH%;%CLASSPATH%;C:\oracle\user_projects\domains\devtwo_domain\winservices
    ...
    rem *** Install the service
    "%WL_HOME%\server\bin\beasvc" -install -svcname:"beasvc %DOMAIN_NAME%_%SERVER_NAME%" -stopclass:ServerStopper -javahome:"%JAVA_HOME%" -execdir:"%USERDOMAIN_HOME%" -maxconnectretries:"%MAX_CONNECT_RETRIES%" -host:"%HOST%" -port:"%PORT%" -extrapath:"%EXTRAPATH%" -password:"%WLS_PW%" -cmdline:%CMDLINE%  -log:"c:\oracle\user_projects\domains\devtwo_domain\servers\AdminServer\logs\AdminServer-stdout.log"
    
  4. Create ServerStopper.java with the following contents in your winservices folder

    import java.io.File;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.Set;
    import java.util.Iterator;
    import java.rmi.RemoteException;
    import javax.naming.Context;
    import javax.management.ObjectName;
    import weblogic.jndi.Environment;
    import weblogic.management.MBeanHome;
    import weblogic.management.WebLogicMBean;
    import weblogic.management.configuration.ServerMBean;
    import weblogic.management.runtime.ServerRuntimeMBean;
    import weblogic.management.runtime.ServerStates;
    import weblogic.management.WebLogicObjectName;
    public class ServerStopper {
      public static void stop() throws Exception {
        MBeanHome home = null;
        \\url of the Admin server
        String url = "t3:\\localhost:7001";
        String username = "weblogic";
        String password = "password";
        ServerRuntimeMBean serverRuntime = null;
        Set mbeanSet = null;
        Iterator mbeanIterator = null;
        try {
          \\ Set ContextClassloader to prevent assertions
          URL[] urls = { new File("\").toURL() };
          Thread.currentThread().setContextClassLoader(new
             URLClassLoader(urls));
          Environment env = new Environment();
          env.setProviderUrl(url);
          env.setSecurityPrincipal(username);
          env.setSecurityCredentials(password);
          Context ctx = env.getInitialContext();
          home = (MBeanHome)
               ctx.lookup("weblogic.management.adminhome");
          mbeanSet = home.getMBeansByType("ServerRuntime");
          mbeanIterator = mbeanSet.iterator();
          while(mbeanIterator.hasNext()) {
            serverRuntime = (ServerRuntimeMBean)mbeanIterator.next();
            if(serverRuntime.getState().equals(ServerStates.RUNNING)){
                serverRuntime.shutdown();
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    
  5. Compile the class file by opening a command prompt and performing the following

    cd C:\oracle\user_projects\domains\devtwo_domain\winservices
    set CLASSPATH=c:\wlserver_10.3\server\lib\weblogic.jar
    javac ServerStopper.java
    

    If you are working with Weblogic 12c, your weblogic.jar will be in a location something like:
    C:\wlserver_12.1\server\lib\weblogic.jar

    Note: the java class file contains some deprecated items. so you will see some warnings but it should be OK for now:

6. Run createAdminWinService.cmd, open a command prompt as Administrator and run the file – example:

cd c:\oracle\user_projects\domains\devtwo_domain\winservices
createAdminWinService.cmd

7. You should now have a service in your Windows Services a service called “beasvc domainname_AdminServer” (in later versions of Weblogic like 12c it will start with something like wlsvc – see image below)

8. Test the service by starting it and accessing the Weblogic Administration Console.
Also test stopping the service through Windows Services.