Tomcat使用JMX管理方式,在Tomcat的自帶應(yīng)用manager就是使用了JMX方式來(lái)管理Tomcat,以此完成Web應(yīng)用的動(dòng)態(tài)部署、啟動(dòng)、停止。
然而manager應(yīng)用是一種本地使用JMX接口的方式。對(duì)于其它的遠(yuǎn)程客戶(hù)端該 怎么做呢?
?
方式1:JConsole客戶(hù)端:
1)設(shè)置環(huán)境變量CATALINA:
|
|
|
|
|
|
set CATALINA_OPTS=-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
|
這是Windows的設(shè)置方式。
Linux /Unix 設(shè)置方式:export?CATALINA_OPTS=-Dcom.sun.management.jmxremote' 'CATALINA_OPTS=-Dcom.sun.management.jmxremote' 'CATALINA_OPTS=-Dcom.sun.management.jmxremote' '-Dcom.sun.management.jmxremote.authenticate=false
網(wǎng)上有很多人說(shuō)是設(shè)置JAVA_OPTS,建議不要這么做。
2)啟動(dòng)Tomcat
3)打開(kāi)JConsole,設(shè)置遠(yuǎn)程連接地址:tomcat_ip:9999?
tomcat_ip 是Tomcat所在機(jī)器的IP,9999就是上面設(shè)置的端口。
?
方式二:使用Ant build.xml
具體使用方式參見(jiàn):http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html#JMXAccessorGetTask:__get_attribute_value_Ant_task
原理同下。
?
方式三:在Java代碼中使用Ant Task
GetTask task=
new
GetTask();
task.setURL(
"http://tomcat_ip:9999/manager"
);
task.setUser(
"xxx"
);
task.setPassword(
"xxx"
);
Project pro
=
new
Project();
task.setOutproperty(
"output"
);
task.setProject(pro);
task.execute();
String responseText
=project.getProperty("output");
原理:使用url連接訪問(wèn)manager應(yīng)用,從而manager使用JMX管理。
?
方式四:在Java代碼中直接訪問(wèn)manager應(yīng)用
public
void
tomcatHome(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
//
TODO Auto-generated method stub
URL url=
new
URL("http://localhost:8080/manager/html"
);
HttpURLConnection conn
=
(HttpURLConnection)url.openConnection();
conn.setAllowUserInteraction(
false
);
conn.setDoInput(
true
);
conn.setUseCaches(
false
);
conn.setDoOutput(
false
);
conn.setRequestMethod(
"GET"
);
conn.setRequestProperty(
"Accept-Charset", "utf-8"
);
conn.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded"
);
String username
="admin"
;
String password
="admin"
;
String authoInfo
=
new
String(Base64.encode((username + ":" +
password).getBytes()));
conn.setRequestProperty(
"Authorization", "Basic "+
authoInfo);
conn.setRequestProperty(
"Connection", "keep-alive"
);
conn.connect();
InputStream input
=
conn.getInputStream();
response.setContentType(
"text/html;charset=UTF-8"
);
PrintWriter out
=
response.getWriter();
byte
[] bs=
new
byte
[1024
];
int
len=-1
;
while
((len=input.read(bs))!=-1
){
out.write(
new
String(bs, 0
, len));
}
out.flush();
out.close();
}
原理同上。
?
方式五:在Java中直接使用JMX API訪問(wèn)
參見(jiàn)我 前面的博文 。當(dāng)然了,這種方式,網(wǎng)絡(luò)上也是常見(jiàn)的。
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

