3 Setting Up a WebLogic Server Instance as a Windows Service
For each server instance that you set up as a Windows service, WebLogic Server creates a key in the Windows Registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
. The registry entry contains such information as the name of the server and other startup arguments.
When you start the Windows host, the Microsoft Management Console uses the information in the Windows Registry key to invoke the weblogic.Server
main class. The MMC cannot be configured to use Node Manager to start Managed Servers, therefore the Node Manager's monitoring and automatic restart features cannot be used for servers that run as a Windows service.
The following tasks set up and manage WebLogic Server instances that run as Windows services:
Setting Up a Windows Service: Main Steps
Windows services enable you to create long-running executable applications that run in the background without any user interface. You can set up a Windows service by running a server-specific script that invokes the weblogic.Server main class by using the information in the Windows registry key. Before running the server-specific script, you must be sure to configure or enable the required connections and java classes for the Administration Server and Managed Servers.
To set up a Windows service:
- Create a script that sets values for server-specific variables and then calls a WebLogic Server master script. See Creating a Server-Specific Script.
- If you are installing a Managed Server as a Windows service, add a variable to the server specific script that specifies the location of the domain's Administration Server. See Configuring a Connection to the Administration Server.
- If you set up both an Administration Server and a Managed Server to run as Windows services on the same computer, modify the WebLogic Server master script so that the Managed Server starts only after the Administration Server finishes its startup cycle. See Requiring Managed Servers to Start After Administration Servers.
- If you want a server instance to shut down gracefully when you stop the Windows service, create a Java class and modify the master script so that the Microsoft Management Console will invoke the class. See Enabling Graceful Shutdowns.
- If you want to see the messages that a server instance prints to standard out and standard error (including stack traces and thread dumps), redirect standard out and standard error to a file. See Redirecting Standard Out and Standard Error to a File.
- If you have created additional Java classes that you want the WebLogic Server instance to invoke, add them to the server's classpath. See Adding Classes to the Classpath.
- Run the server-specific script. See Run the Server-Specific Script.
Creating a Server-Specific Script
The script that you create must set values for variables that identify the name of the server instance and other server-specific information. Then it must call a master script, WL_HOME
\server\bin\installSvc.cmd
, where WL_HOME
is the directory in which you installed WebLogic Server. The master scripts invokes the wlsvc
utility, which adds a key to the Windows Registry.
Note:
You can specify a service description when installing a Windows service in the wlsvc
utility by using the -svcdescription:
<user_specified_service_description>
option. You can also specify a service description by setting the environment variable SERVICE_DESCRIPTION
prior to running your installSvc.cmd
script.
For more information about wlsvc
, enter the following command at a command prompt: WL_HOME
\server\bin\wlsvc -help
, where WL_HOME
is the directory in which you installed WebLogic Server.
To see an example of a server-specific script, refer to Example 3-1.
To create a server-specific script:
Configuring a Connection to the Administration Server
If you want to install a Managed Server as a Windows service, you must include a variable that specifies the location of the domain's Administration Server. The Managed Server must contact the Administration Server to retrieve its configuration data.
The Administration Server (which is not a service) must be started before installing and starting Managed Server as a Windows service.
To configure a connection to the Administration Server:
Example 3-1 Example Script for Setting Up a Managed Server as a Windows Service
echo off SETLOCAL set MW_HOME=C:\Oracle\Middleware\Oracle_Home set DOMAIN_NAME=base_domain set USERDOMAIN_HOME=C:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain set SERVER_NAME=ManagedServer set ADMIN_URL=http://10.xxx.xx.xx:7001 set WL_HOME=C:\Oracle\Middleware\Oracle_Home\wlserver set DOMAIN_PRODUCTION_MODE=true set USER_MEM_ARGS=-Xms128m -Xmx512m call "C:\Oracle\Middleware\Oracle_Home\user_projects\domains\base_domain\bin\setDomainEnv.cmd" call "C:\Oracle\Middleware\Oracle_Home\wlserver\server\bin\installSvc.cmd" ENDLOCAL
Note:
To use this example script to set up an Administration Server as a Windows service, remove the ADMIN_URL
variable.
If you copy and paste examples, make sure that there are no trailing spaces in them.
WLS_USER
and WLS_PW
are optional and can be added to the script only if boot identity file is not being used. See Creating a Server-Specific Script.
Requiring Managed Servers to Start After Administration Servers
If you set up both an Administration Server and a Managed Server to run as Windows services on the same computer, you can specify that the Managed Server starts only after the Administration Server.
To require a Managed Server to start after the Administration Server Windows service:
-
Create a backup copy of the
WL_HOME
\server\bin\installSvc.cmd
master script. -
If you have already installed the Administration Server as a Windows service, remove the service. See Removing a Server as a Windows Service.
-
Before you install (or reinstall) the Administration Server as a Windows service, do the following:
-
In a text editor, open the
WL_HOME
\server\bin\installSvc.cmd
master script.The last command in this script invokes
wlsvc
, which is the WebLogic Server utility that modifies the Windows Registry. -
In
installSvc.cmd
, add the following argument to the command that invokes thewlsvc
utility:-delay:delay_milliseconds
This specifies the number of milliseconds to wait before the Microsoft Management Console changes the service status from
SERVER_START_PENDING
toSTARTED
.For example, if your Administration Server requires 2 minutes to complete its startup cycle and begin listening for requests, then specify
-delay=120000
. When you boot the Windows host computer, the Microsoft Management Console reports a status ofSERVER_START_PENDING
for 2 minutes. Then it changes the status toSTARTED
.The modified
wlsvc
invocation for the Administration Server will resemble the following:"%WL_HOME%\server\bin\wlsvc" -install -svcname:"%DOMAIN_NAME%_%SERVER_NAME%" -delay:120000 -javahome:"%JAVA_HOME%" -execdir:"%USERDOMAIN_HOME%" -extrapath:"%WL_HOME%\server\bin" -password:"%WLS_PW%" -cmdline:%CMDLINE%
For more information about
wlsvc
, enter the following command at a command prompt:WL_HOME
\server\bin\wlsvc -help
, whereWL_HOME
is the directory in which you installed WebLogic Server.
-
-
Install the Administration Server Windows service.
-
Before you install the Managed Server as a Windows service, do the following:
-
In a text editor, open the
WL_HOME
\server\bin\installSvc.cmd
master script. -
In
installSvc.cmd
, add the following argument to the command that invokes thewlsvc
utility:-depend:Administration-Server-service-name
where
Administration-Server-service-name
is the name of the Administration Server Windows service. To verify the service name, look in the Microsoft Management Console under Services.With this option, the Microsoft Management Console will wait for the Administration Server Windows service to report a status of
STARTED
before it starts the Managed Server Windows service.For example, the modified
wlsvc
invocation for the Managed Server will resemble the following:"%WL_HOME%\server\bin\wlsvc" -install -svcname:"%DOMAIN_NAME%_%SERVER_NAME%" -depend:"wlsvc myDomain_myAdminServer" -javahome:"%JAVA_HOME%" -execdir:"%USERDOMAIN_HOME%" -extrapath:"%WL_HOME%\server\bin" -password:"%WLS_PW%" -cmdline:%CMDLINE%
You can also add the
-delay:delay_milliseconds
option to a Managed Server Windows service if you want to configure when the Microsoft Management Console reports a status ofSTARTED
for the service.
-
Enabling Graceful Shutdowns
By default, if you use the Microsoft Management Console to stop a server instance, it kills the server's Java Virtual Machine (JVM). If you kill the JVM, the server immediately stops all processing. Any session data is lost. If you kill the JVM for an Administration Server while the server is writing to the config.xml
file, you can corrupt the config.xml
file.
To enable graceful shutdowns from Microsoft Management Console:
-
Create a Java class that invokes the
weblogic.management.runtime.ServerRuntime.shutdown()
method.This method gracefully shuts down a server after the server has completed all in-flight work. For an example of such a class, refer to Java Class that Shuts Down a Server Instance.
-
Create a backup copy of the
WL_HOME
\server\bin\installSvc.cmd
master script. -
In a text editor, open the
WL_HOME
\server\bin\installSvc.cmd
master script and do the following:-
Add the class that you created to the
set CLASSPATH
statement.For example if you archived your class in a file named
c:\myJar
, the modified statement will be as follows:set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%WL_HOME%\server\lib\weblogic_sp .jar;%WL_HOME%\server\lib\weblogic.jar;c:\myJar;%CLASSPATH%
-
Add the following argument to the last line of the script, which calls the
wlsvc
utility:-stopclass:javaclass
where
javaclass
is the full classpath name of the class that you created. This argument loadsjavaclass
and then invokes itspublic void static stop()
method.For example, if you packaged the class in Example 3-2 in
com.myClasses
, the modifiedwlsvc
command will be as follows:"%WL_HOME%\server\bin\wlsvc" -install -svcname:"%DOMAIN_NAME%_%SERVER_NAME%" –stopclass:com.myClasses.ServerStopper -javahome:"%JAVA_HOME%" -execdir:"%USERDOMAIN_HOME%" -extrapath:"%WL_HOME%\server\bin" -password:"%WLS_PW%" -cmdline:%CMDLINE%
For more information about
wlsvc
, enter the following command at a command prompt:WL_HOME
\server\bin\wlsvc -help
, whereWL_HOME
is the directory in which you installed WebLogic Server.
-
-
In the WebLogic Server Administration Console, on the server's Control page, configure the Managed Server's graceful shutdown behavior.
You can determine whether a graceful shutdown operation drops all HTTP sessions immediately and you can configure the amount of time that a graceful shutdown operation waits before forcing a shut down. See Control graceful shutdowns in the Oracle WebLogic Server Administration Console Online Help.
-
Consider modifying the default timeout value that the Windows service specifies.
By default, when you use the Microsoft Management Console to stop a Windows service, it waits 30 seconds for the service to stop before it kills the service and prints a timeout message to the System event log.
If you use
-stopclass
to gracefully shut down a server, 30 seconds might not be enough time for the server to gracefully end its processing.To configure a timeout period, create a
REG_DWORD
registry value namedServicesPipeTimeout
under the following registry key:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control
The key value must be in milliseconds.
This value is read from the registry during the startup of the Windows operating system and it affects all services that are installed.
-
Save your changes to the WebLogic Server master script.
Java Class that Shuts Down a Server Instance
The following Java class uses Java Management Extensions (JMX) to shut down a server instance. Each server uses JMX Managed Beans (MBeans) to expose its management attributes and operations. One such MBean, ServerRuntime
, exposes a shutdown()
method that gracefully shuts down a server.
The class in Example 3-2 uses the Administration MBeanHome
interface, which can retrieve and call ServerRuntime
MBean operations for all server instances in a domain.
See Developing Custom Management Utilities Using JMX for Oracle WebLogic Server and Javadoc
.
Example 3-2 Java Class that Shuts Down a Server Instance
package com.myClasses;
import java.net.*;
import java.io.*;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
public class ServerStopper {
public static void stop() {
try {
String servername = "AdminServer";
String hostname = "localhost";
int port = 7001;
String username = "weblogic";
String password = "password";
//Select t3 or iiop as required
//String protocol = "t3";
String protocol = "iiop";
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
// To avoid AssertionError
URL[] urls = { new File("/").toURL() };
Thread.currentThread().setContextClassLoader(new
URLClassLoader(urls));
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
MBeanServerConnection connection = connector.getMBeanServerConnection();
ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
ObjectName[] serverRuntimes = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
for (ObjectName serverRuntime : serverRuntimes) {
String name = (String) connection.getAttribute(serverRuntime, "Name");
if (name.equals(servername)) {
String state = (String) connection.getAttribute(serverRuntime, "State");
if (state.equals("RUNNING") || state.equals("ADMIN")) {
connection.invoke(serverRuntime, "shutdown", new Object[0], new String[0]);
System.out.println("Stopped the server: " + servername);
}
else
System.out.println("The server is not in RUNNING or ADMIN state. The current state is: " + state);
return;
}
}
System.out.println("The server, " + servername + " does not exist");
connector.close();
}
catch (Exception ex) {
Logger.getLogger(ServerStopper.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Redirecting Standard Out and Standard Error to a File
By default, when you install a WebLogic Server instance as a Windows service, you cannot see the messages that the server or its JVM print to standard out and standard error.
To view these messages, you must redirect standard out and standard error to a file:
After you install the service and restart the Windows host, to view the messages that the server writes to standard out or standard error, do one of the following:
-
Make a copy of the file that you specified and view the copy. The Windows file system cannot write to files that are currently opened.
-
To view the messages as they are being printed to the file, open a command prompt and, using a DOS utility that supports the
tail
command, entertail -f
stdout-filename
.
Changing the Default Rotation Criteria
By default, every 24 hours the Windows service archives messages to a file named pathname-yyyy_mm_dd-hh_mm_ss
. New messages collect in the file that you specified when you set up the service.
You can change the time interval or you can set up rotation to occur based on the size of the message file instead of a time interval.
To change the default criteria at which the Windows service rotates message files:
To cause the WebLogic Server instance to print a thread dump to standard out, do either of the following:
-
Use the WLST
threadDump
command. -
Open a command prompt and enter the following command:
WL_HOME
\bin\wlsvc -dump -svcname:
service-name
where
WL_HOME
is the directory in which you installed WebLogic Server andservice-name
is the Windows service that is running a server instance.
For example:
D:\Oracle\Middleware\wlserver_12.1\server\bin\wlsvc -dump -svcname:mydomain_myserver
Adding Classes to the Classpath
The classpath is a declaration of the location of Java classes that a JVM can invoke. When you use the WebLogic Server master script to install a server instance as a Windows service, the master script specifies all classes required to run a server instance. If you want to extend WebLogic Server by adding your own Java classes, you must add them to the classpath.
To add classes to the classpath:
-
Create a backup copy of the
WL_HOME
\server\bin\installSvc.cmd
master script. -
In a text editor, open the
WL_HOME
\server\bin\installSvc.cmd
master script. -
Add your class to the
set CLASSPATH
statement.For example if you archived your class in a file named
c:\myJar
, the modified statement will be as follows:set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%WL_HOME%\server\lib\weblogic_sp. jar;%WL_HOME%\server\lib\weblogic.jar;c:\myJar;%CLASSPATH%
Note:
Win32 systems have a 2K limitation on the length of the command line. If the classpath setting for the Windows service startup is very long, the 2K limitation could be exceeded.
To work around this limitation:
-
Place the value of the
set CLASSPATH
command in a separate text file. -
In the
WL_HOME
\server\bin\installSvc.cmd
master script, find theset CMDLINE
command. -
Within the
set CMDLINE
command, replace the-classpath \"%CLASSPATH%\"
option with the following option:-classpath @
pathname\filename
where
pathname\filename
is the absolute path and name of the file that contains the classpath values.For example:
set CMDLINE="%JAVA_VM% %MEM_ARGS% %JAVA_OPTIONS% -classpath @c:\myClasspath.txt -Dweblogic.Name=%SERVER_NAME% -Dbea.home=\"D:\bea_70sp2\" -Dweblogic.management.username=%WLS_USER% -Dweblogic.management.server=\"%ADMIN_URL%\" -Dweblogic.ProductionModeEnabled=%STARTMODE% -Djava.security.policy=\"%WL_HOME%\server\lib\weblogic.policy\" weblogic.Server"
-
-
Save your changes to the WebLogic Server master script.
Run the Server-Specific Script
Note:
To run the server-specific script, you must log in to the Windows computer with a user account that has privileges to modify the Windows registry.
If you install the Windows service in a production environment, Oracle recommends that you do not run the service under an operating-system user account that has administrator-level privileges. See Verifying the User Account Under Which the Service Runs.
To run the server-specific script:
Verifying the Setup
Use the wlsvcx64 -debug
command to verify that the Windows service is configured correctly.
To verify that you successfully set up a WebLogic Server as a Windows service, do the following:
Verifying the User Account Under Which the Service Runs
In a production environment, WebLogic Server Windows services should run under a special operating-system user account that has limited access privileges. For example, the OS user should have access privileges only to Oracle files and to your domain files. This should be the only user account that has access to these files.
To ensure that the WebLogic Server instance runs under the special OS user account:
-
Open Services in the Microsoft Management Console.
For example, from the Windows XP desktop:
-
Select the Start menu.
-
On the Start menu, select Programs > Administrative Tools > Services.
-
-
In the Services window, right click the WebLogic Server Windows service and select Properties.
-
In the Properties window, select the Log On tab.
-
Under Log on, select This account. Then enter the user name and password of the special OS user account.
-
Click OK.
Note:
When accessing network drives, the Windows service must run under the same user name as the one who shared the network drive.
Using the Services Window to Stop or Restart a Server Instance
Use the Microsoft Management Console Services window to stop and restart a server instance.
By default, if you use the Microsoft Management Console to stop a server instance, it kills the server's Java Virtual Machine (JVM). If you kill the JVM, the server immediately stops all processing. Any session data is lost. If you kill the JVM for an Administration Server while the server is writing to the config.xml
file, you can corrupt the config.xml
file. See Enabling Graceful Shutdowns.
To stop or restart a WebLogic Server instance that is installed as a Windows service:
- Select Start > Programs > Administrative Tools > Services.
- In the Services window, find the service that you created. By default, the service name starts with
wlsvc
. - Right-click the service name and select commands from the menu.
Removing a Server as a Windows Service
You can remove a Windows service by using a script that causes the wlsvc utility to remove the associated key from the Windows Registry. It does not affect the server instance's configuration that is saved in the domain's configuration file. After removing the Windows service, you can restart the WebLogic server instance with start scripts, for Managed Servers or Node Manager.
The script sets values for variables that identify the name of the server instance and other server-specific information. Then the script calls a master uninstall script, WL_HOME
\server\bin\uninstallSvc.cmd
, where WL_HOME
is the directory in which you installed WebLogic Server. The master scripts invokes the wlsvc
utility, which removes a key from the Windows Registry.
To see an example of a server-specific uninstaller script, refer to Example 3-3.
To create a script for removing a Windows service that runs a WebLogic Server instance:
mydomain_myserver removed
By default, standard out is the command prompt in which you run the batch
file.
Example 3-3 Script to Remove a Windows Service
echo off SETLOCAL set DOMAIN_NAME=myWLSdomain set SERVER_NAME=myWLSserver set MW_HOME=D:\Oracle\Middleware\Oracle_Home call "D:\Oracle\Middleware\Oracle_Home\wlserver\server\bin\uninstallSvc.cmd" ENDLOCAL
Changing Startup Credentials for a Server Set Up as a Windows Service
To make a WebLogic Server instance run under different user credentials, you can change a Windows service configuration to support various startup credentials.
Do one of the following:
-
If you set up the Windows service to retrieve user names and passwords from a boot identity file, you can overwrite the existing file with a new one that contains the new user name and password. You must specify the name of an existing user in the WebLogic Server default security realm. See Boot Identity Files.
-
If you set up the Windows service to retrieve user names and passwords from the Windows registry, then you must remove the Windows service and create a new one that uses your new user name or password:
-
Uninstall the Windows service that runs the WebLogic Server instance. See Removing a Server as a Windows Service.
-
In a text editor, open the script that you used to install the service and enter the new user name and password as the value for the
set WLS_USER
andset WLS_PW
commands. WebLogic Server encrypts these values in the Windows Registry. -
Save your modifications to the script.
-
Enter the name of the server-specific script.
The command prompt runs the script as a
batch
file.If the script runs successfully, it creates a Windows service named
mydomain_myserver installedDOMAIN_NAME_SERVER_NAME
and prints a line to standard out that is similar to the following:By default, standard out is the command prompt in which you run the server-specific
batch
file. -
(Optional) Remove the user name and password from the script file.
-