Monitor the discussion of a Web Service (if the Weblogic service dies, enable it automatically)

Source: Internet
Author: User

Recently, our web services always die automatically. For this reason, the boss asked us to take turns on duty and always watch the web service. If the Web service dies, we need to immediately start it.
In this way, it is difficult for the masses to be hurt. No one is willing to be on duty. What should I do?
I thought for a moment, isn't it possible to write a program that monitors Web Services?
There are multiple monitoring methods:
1: traditional desktop programs.
2: B/S monitoring program.

First, test a traditional desktop program.
First, perform a test to check whether foxmail.exe is running. If it is not running, start it.
In the system progress, foxmail.exeis called foxmail.exe. if there is a process named foxmail.exe, this will take any action; otherwise, start it.

//---------------------------------------------------------------------------

# Include <VCL. h>
# Pragma hdrstop
# Include "tlhelp32.h"
# Include "unit1.h"
//---------------------------------------------------------------------------
# Pragma package (smart_init)
# Pragma resource "*. DFM"
Tform1 * form1;
//---------------------------------------------------------------------------
_ Fastcall tform1: tform1 (tcomponent * owner)
: Tform (owner)
{
}
//---------------------------------------------------------------------------

Void _ fastcall tform1: button1click (tobject * sender)
{
Timer1-> enabled = true;
}
//---------------------------------------------------------------------------

Void _ fastcall tform1: timer1timer (tobject * sender)
{
Try {
Handle snapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (snapshot = invalid_handle_value)
Return;
Processentry32 processinfo;
Processinfo. dwsize = sizeof (processinfo );
Bool status = process32first (snapshot, & processinfo );
Bool flag = false; // There Is A foxmail.exe Process
While (Status)
{
If (ansistring (processinfo. szexefile). lowercase () = "foxmail.exe ")
{
Flag = true;
}
Status = process32next (snapshot, & processinfo );
}
Ansistring temptime = formatdatetime ("YYYY, mm, DD, HH: NN: SS", now ());
If (! Flag ){
Ansistring exename = "D: // program files // Foxmail // foxmail.exe ";
ShellExecute (handle, "open", exename. c_str (), "", "", sw_show );
Memo1-> lines-> Add ("restart this program at" + temptime );
}
Else {
Memo1-> lines-> Add ("running at" + temptime );
}
Closehandle (snapshot );
} Catch (...){

}
}
//---------------------------------------------------------------------------

Void _ fastcall tform1: button2click (tobject * sender)
{
Timer1-> enabled = false;
}
//---------------------------------------------------------------------------

This program can successfully monitor the operation of foxmail.exe. if foxmail.exe is stopped, it will be automatically started.

However, our web service program is java.exe in the process, and there are two. What should we do?
There is a similar method: if there are two java.exeentries in the process, the Web service is good. If java.exe is less than two, the Web service is stopped and needs to be restarted.
After modification, the following program is obtained:
Void _ fastcall tform1: button1click (tobject * sender)
{
I = strtoint (edit1-> text); // I is the global variable, and the number of Java records should be 2.
Timer1-> enabled = true;
}
//---------------------------------------------------------------------------
Void _ fastcall tform1: timer1timer (tobject * sender)
{
Try {
Handle snapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
If (snapshot = invalid_handle_value)
Return;
Processentry32 processinfo;
Processinfo. dwsize = sizeof (processinfo );
Bool status = process32first (snapshot, & processinfo );
Int J = 0; // record the number of java.exe at the beginning
While (Status)
{
If (ansistring (processinfo. szexefile). lowercase () = "java.exe ")
{
J ++;
}
Status = process32next (snapshot, & processinfo );
}
Ansistring temptime = formatdatetime ("YYYY, mm, DD, HH: NN: SS", now ());
If (j <I ){
Ansistring exename = "C: // Bea // user_projects // domains // hbep // autostart. cmd ";
ShellExecute (handle, "open", exename. c_str (), "", "", sw_show );
Memo1-> lines-> Add ("restart this program at" + temptime );
}
Else {
Memo1-> lines-> Add ("running at" + temptime );
}
Closehandle (snapshot );
} Catch (...){
}
}

Why is java.exe used in the system's input table when any Java program is running? What should I do? (Although our servers are clean, we basically do not run other Java programs ).

Use another method: XMLHTTP technology:
My idea is as follows:
Our web server is weblogic. I deploy another tomcat server on the server and write a servlet in the two web services respectively,
Then write an HTML file (No step is required) and use XMLHTTP to access the Weblogic servlet (assumed as a). If a responds, it indicates that the Weblogic service is running, if no response is received for five consecutive times
The WebLogic service is declared dead. At this time, an XML command is sent to the Tomcat Servlet (assumed as B) to let the servlet execute the following operations to start the Weblogic service:
Runtime.getruntime(cmd.exe C ("CMD/C start C: // Bea // user_projects // domains // cmdp // autostart. cmd ");

The HTML file is as follows (use XMLHTTP to send XML commands to servlet ):
<Body>
<Script language = "JavaScript">
VaR xml = "<root> <Test> ask </test> </root>" // defines a ask command and sends it to the Servlet
VaR xmlrestart = "<root> <Test> restart </test> </root>"
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp ");
VaR xmldoc = new activexobject ("msxml2.domdocument ");

Function readxml (myxml) {// parse client XML
Xmldoc. loadxml (myxml );
If (xmldoc. parseerror. Line> 0 ){
Throw xmldoc. parseerror. reason;
}
VaR nodes = xmldoc. selectnodes ("/root/test ");
For (VAR I = 0; I <nodes. length; I ++ ){
Return (nodes. Item (I). Text );
}
}

Function action () {// communication between the client and the server
XMLHTTP. Open ("Post", "http: // 127.0.0.1: 7001/xmlhttptest/urltestservlet", false );
XMLHTTP. setRequestHeader ("context-type", "text/XML; charset = UTF-8 ");
XMLHTTP. Send (XML );
VaR answerxml = XMLHTTP. responsetext;
If (readxml (answerxml ))! = 'OK' {// The servlet response is not received. If Weblogic is disabled, the "restart" command is sent to the Tomcat servlet.
XMLHTTP. Open ("Post", "http: // 127.0.0.1: 8888/xmlhttptest/urltestservlet", false );
XMLHTTP. setRequestHeader ("context-type", "text/XML; charset = UTF-8 ");
XMLHTTP. Send (xmlrestart );
VaR answerxmlfromweblogic = XMLHTTP. responsetext;
}
}

</SCRIPT>
<Button onclick = "Action ();"> send XML to the server and receive Server Response </button>
</Body>
Deploy a servlet in WebLogic as follows:
Package com. LCL;

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util .*;
Import org. dom4j. Io .*;
Import org. dom4j .*;

/**
* @ Author LCL
*
* To change the template of the type comment generated by todo, go
* Window-preferences-Java-code style-Code Template
*/

Public class urltestservlet extends httpservlet {
Private Static final string content_type = "text/XML; charset = UTF-8 ";
// Initialize global variables
Public void Init () throws servletexception {
}
// Process the http get request

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype (content_type );
Request. setcharacterencoding ("UTF-8 ");
Printwriter out = response. getwriter ();
Inputstream is = request. getinputstream ();

Saxreader reader = new saxreader ();
Document Doc = NULL;
Try {
Doc = reader. Read (is );
} Catch (exception ex ){
System. Out. println (Ex );
}
String [] S = getelementtexts (Doc, "test ");
Out. println ("<root> ");
Out. println ("<Test> ");
Out. println ("OK ");
Out. println ("</test> </root> ");
}

Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}

Private string getelementtext (document DOC, string name ){
Return Doc. getrootelement (). element (name). gettext ();
}

Private string [] getelementtexts (document DOC, string name ){
List L = Doc. getrootelement (). Elements (name );
Iterator it = L. iterator ();
List L1 = new partition list ();
While (it. hasnext ()){
Element E = (element) it. Next ();
L1.add (E. gettext ());
}
Return (string []) l1.toarray (New String [] {});
}

// Clean up resources
Public void destroy (){
}
}

The Web. xml changes of Weblogic are as follows (adding servlet ing ):

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
<Display-Name> welcome to Tomcat </display-Name>
<Description>
Welcome to Tomcat
</Description>

<! -- Jspc servlet mappings start -->

<Servlet>
<Servlet-Name> urltestservlet </servlet-Name>
<Servlet-class> com. LCL. urltestservlet </servlet-class>
</Servlet>
 

<Servlet-mapping>
<Servlet-Name> urltestservlet </servlet-Name>
<URL-pattern>/urltestservlet </url-pattern>
</Servlet-mapping>

<! -- Jspc servlet mappings end -->
<Welcome-file-List>
<Welcome-File> index.htm </welcome-File>
</Welcome-file-List>
</Web-app>

Tomcat's servlet is similar to Weblogic's servlet, but after receiving the restart command, execute:
Runtime.getruntime(cmd.exe C ("CMD/C start C: // Bea // user_projects // domains // cmdp // autostart. cmd ");

OK. Now you can rest assured.
You can even start Weblogic and tomcat.
No duty now.
It is hereby archived.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.