View resource usage in Linux-WebService port monitoring

Source: Internet
Author: User

I found a serious problem in my recent project.

 

In an application we wrote, JBoss serves as the application container, where the WebService Service is loaded. After a service is running for a period of time, it may be a few days later. The information we see on the JBoss console shows that multiple services hosted by JBoss are still running, but this WebService is down.

 

Access the corresponding URL address, prompting timeout

 

Later I wrote a program to monitor the URL port. The main code is as follows:

 

Package com. aspire. MECs. Engine. WebService;

// Generate the required data records according to the Report Format

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. util. date;

Import java.net. httpurlconnection;
Import java.net. url;

Import java. SQL .*;

Import org. Apache. log4j. Logger;

Import com. aspire. MECs. Engine. Dao. querydao;
Import com. aspire. MECs. Engine. Unsub. unsubprocessmonitor;
Import com. aspire. MECs. Engine. util .*;
Import com. aspire. MECs. Engine. VO. smsnotifyvo;

Public class webservicemonitor implements runnable {
Private Static logger log = logger. getlogger (webservicemonitor. Class );
 
Connection conn = NULL;
 
Private string smsinfomsg = configutils. getvalue ("momu", "monitorsmsinfo ");
 
// Private string smsinfomsg = "the Sichuan Experience card system has detected an error in the webserivce interface. Please check related services immediately. -- Sichuan Experience card ";

Private string webservice_url = configutils. getvalue ("momu", "monitorurl ");

// Private string webservice_url = "http: // 211.137.86.211: 8888/services/mobilemecs? WSDL "; // monitored WebService address

// Private int timeoutcounts = 3; // you can specify the number of timeout alarms.
Private int timeoutcounts = integer. parseint (configutils. getvalue ("momu", "timeoutcounts "));

// Private long request_timeout = 10000; // interval of heartbeat. The interval of this event is a long time to simulate a client request to access the WebService address.
Private long request_timeout = long. parselong (configutils. getvalue ("momu", "request_timeout "));
 
 

// The twebservicemonitormobile monitoring number is placed in this table of the database, and the condition isvalid is 0

Private Static webservicemonitor instance = NULL;
 

Public static webservicemonitor getinstance (){
Return instance! = NULL? Instance
: (Instance = new webservicemonitor ());
}

Public webservicemonitor (){

}

Public synchronized Boolean isurlrunning (string URL ){
Boolean flag = true;

URL urlstr;

Httpurlconnection connection = NULL;

Int state = 0;

Try {
Urlstr = new URL (URL );

Connection = (httpurlconnection) urlstr. openconnection ();
System. setproperty ("sun.net. Client. defaulttimetimeout", "5000 ");
System. setproperty ("sun.net. Client. defaultreadtimeout", "5000 ");

State = connection. getresponsecode ();
If (State = 200 ){
Log.info ("WebService monitoring is normal: After requesting the WebService monitoring address, the returned result is:" + State );
Flag = true;
} Else {
Log.info ("WebService monitoring exception: After requesting the WebService monitoring address, the returned result is:" + State );
Flag = false;
}

} Catch (exception ex ){
Log.info ("WebService monitoring exception: After requesting the WebService monitoring address, the system returns the exception message:" + State );
// Ex. printstacktrace ();
Flag = false;
} Finally
{
Connection. Disconnect ();

}

Return flag;
}

 

 
 
Private Static connection getconn () throws exception {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Connection conn = drivermanager. getconnection (
"JDBC: oracle: thin: @ 10.1.3.80: 1521: ora9i", "mecs_hx", "mecs_hx ");
Return conn;
}

Public void run (){
Try {
// This thread runs once a day at nine o'clock A.M. every day
// Conn = getconn ();

Int counts = timeoutcounts;

Boolean breakflag = false;

While (true ){
Conn = dB. getconnection ();

Log.info ("timeoutcounts:" + timeoutcounts );
Log.info ("request_timeout:" + request_timeout );

If (counts> 0)
{

Boolean flaga = isurlrunning (webservice_url );

If (! Flaga)
{
Counts --;
Log.info ("WebService monitor: exception % >_<%, monitoring variable counts --, the current counts value is:" + counts );
} Else
{
Counts = timeoutcounts;
Log.info ("WebService monitor: normal ^ _ ^, the monitoring parameter counts is set to the initial value: timeoutcounts ");
}

} Else
{
// Send SMS monitoring information
Log.info ("send SMS monitoring information ");

Statement SMT = conn. createstatement ();
String SQL = "select mobile from twebservicemonitormobile where isvalid = '0' order by ID ASC ";
Resultset rst=smt.exe cutequery (SQL );
String mobile = "";
While (RST. Next ())
{
Mobile = RST. getstring (1 );
String servcode = configutils. getvalue ("Submit", "srcid ");

Smsnotifyvo smsnotif= new smsnotifyvo ();

Smsnow.setcontent (smsinfomsg );
Smsnoodle. setservcode (servcode );
Smsnow.setdestnum (mobile );
Smsnotifyutils. smsnotify (smsnotify );
}
Breakflag = true;


}

If (breakflag)
{
Log.info ("xxx WebService monitoring exception, the monitoring thread is terminated. ");
Thread. currentthread (). Interrupt ();
}


Thread. currentthread (). Sleep (request_timeout );

Try {
DB. Close ();
}
Catch (exception E)
{
Log.info ("disconnection error:" + E. tostring ());
}

}

} Catch (exception e ){
// E. printstacktrace ();

}

}
 
Public void main (string [] Arg)
{


}
 
}

The main purpose of the preceding thread is to notify the maintenance personnel to restart the system after the WebService Service is down to solve the problem, but it is still inconvenient. Sometimes, when the maintenance personnel are sleeping or working outside the office, they have to get the monitoring information and rush to their homes. To put it bluntly, this method is a permanent cure.

 

Later, a buddy said to check the resource usage:

 

Detected during monitoring, wait for a period of time

 

Please remember this command: PS-Ef | grep Java

 

[MECs @ SC _boss01] JBoss/Server/engine> PS-Ef | grep Java
602 31846 1 0 2009? 00:44:30/opt/aspire/product/mecsadmin/jdk142/bin/Java-djava. AWT. headless = true-djava. endorsed. dirs =/opt/aspire/product/mecsadmin/MECs/tomcat/tomcat4134/common/endorsed-classpath/opt/aspire/product/mecsadmin/jdk142/lib/tools. jar:/opt/aspire/product/mecsadmin/MECs/tomcat/tomcat4
MECS 25765 25746 1 00:09:07 pts/8/opt/aspire/product/mecsadmin/jdk142/bin/Java-dprogram. name = run. sh-server-djava. AWT. headless = true-dmecs_conf_path =/opt/aspire/product/MECs/CFG-djava. endorsed. dirs =/opt/aspire/product/MECs/JBoss/lib/endorsed-classpath/opt/aspire/product/MECs/JBoss/bin/run. jar :/
MECS 26482 10714 0 00:00:00 pts/8 grep Java

 


[MECs @ SC _boss01] JBoss/Server/engine> lsof-P 25765 | grep RDB | WC-l
8

Or use the lsof-P 25765 | WC-l command.

 

You can get the usage of system resources.

 

The preceding method can be used to monitor the usage of application resources.

 

After the problem is found, the bufferreader reading the file, the database connection and other objects, the resource usage, and the unstable WebService are released in a timely manner.

Related Article

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.