JBoss 4 - 해당되는 글 3건

아래의 코드를 복사하셔서 web application context에 vm.jsp로 저장하신 후 확인하시면 됩니다.

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import="java.net.InetAddress" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>

<%
  if (request.getParameter("gc") != null) {
    System.gc();
    System.runFinalization();
  }
 Properties p = System.getProperties();
%>

<HTML>
<HEAD>
<META content="text/html; charset=euc-kr" http-equiv=Content-Type>
<!--META http-equiv="Refresh" content="10;url=<%= request.getRequestURI() %>"-->
<link href="style.css" rel=stylesheet type="text/css">
</HEAD>

<body leftmargin=15 topmargin=10>
<center><p>

<table width="600" cellpadding="7" cellspacing="0" border="1" bordercolordark="WHITE" bordercolorlight="BLACK">
<tr><td>
■ HOST : <%= InetAddress.getLocalHost().getHostName() %>
(<%= InetAddress.getLocalHost().getHostAddress() %>)&nbsp;&nbsp;
<% SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z", Locale.CHINA); %>
■ Current Time : <%= formatter.format(new Date()) %>
</td></tr>

<tr><td align=center>

<%
   Runtime rt = Runtime.getRuntime();
   long free = rt.freeMemory();
   long total = rt.totalMemory();
   long usedRatio = (total - free) * 100 / total;
   long unusedRatio = free * 100 / total;
%>

<table width=100% bgcolor="lightgrey" border=1 cellpadding=6 cellspacing=0>
<tr>
<td align="center" colspan="2">Total Java Virual Machine Memory (<b><%= total/1024 %> KB</b>)</td>
</tr>
<tr bgcolor=#E3E3E3>
<td align="center">Used Memory (<b><%= (total - free)/1024 %> KB</b>)</td>
<td align="center">Available Memory (<b><%= free/1024 %> KB</b>)</td>
</tr>
<tr bgcolor=#E8EEEC>
<td><hr color="#CC3366" align=left size=10 width="<%= usedRatio %>%" noshade>
(<%= usedRatio %> %)</td>
<td><hr color="#0066FF" align=left size=10 width="<%= unusedRatio %>%" noshade>
(<%= unusedRatio %> %)</td>
</tr>
</table>

<p>
<a href="<%= request.getRequestURI() %>?gc=">
<img src="trash.gif" valign=middle border=0>&nbsp;Execution GC</a>
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;
<a href="<%= request.getRequestURI() %>">
<img src="refresh.gif" valign=middle border=0>&nbsp;Reload</a>

</td></tr>
</table>

</center>
</BODY>
</HTML>

|
JBoss Web Service Sample을 작성해보도록 하겠습니다. JBoss Web Service는 jboss.org의 jbossws 프로젝트를 이용하여 구성되어 있으며, JBoss Application Server 내에서 jbossws에 대한 sar(service archive)로 포팅되어 있습니다.

보통의 WebService예제는 vendor specific 으로 구성되어 있지만 JBoss의 특징이라면 "무조건 표준으로 간다"(워낙에 표준 준수가 심해서 가끔은 짜증이 납니다)이므로 JBoss Web Service라지만 다른 엔진 라이브러리만 있다면 아무데서나 구동될 수 있습니다.

Eclipse를 띄우고 일반 웹 애플리케이션 프로젝트를 띄운 후 JBoss library를 프로젝트의 Build Path로 지정합니다. 이 부분에 대해서는 별도 설명을 하지 않습니다.

1. Server 측 코드를 작성합니다.

package com.jboss.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * This is a webservice class exposing a method called greet which takes a input
 * parameter and greets the parameter with hello.
 *
 * @author Ji-Woong Choi
 */

/*
 * @WebService indicates that this is webservice interface and the name
 * indicates the webservice name.
 */
@WebService(name = "Hello", targetNamespace="
http://client.jboss.com/", serviceName="GreetService")
/*
 * @SOAPBinding indicates binding information of soap messages. Here we have
 * document-literal style of webservice and the parameter style is wrapped.
 */
//@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL)
public class Hello {
 /**
  * This method takes a input parameter and appends "Hello" to it and returns
  * the same.
  *
  * @param name
  * @return
  */
 @WebMethod
 public String greet(@WebParam(name = "name")
 String name) {
  System.out.println("JBoss Web Service was invoked by " + name);
  return "Hello!, " + name;
 }
}

Web Service Annotation을 이용하여 필요한 메소드들을 구성합니다.

2. Web.xml 파일을 편집합니다. 본 예제에서는 servlet style을 이용하여 처리합니다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xmlns="
http://java.sun.com/xml/ns/javaee"
 xmlns:web="
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>jboss-ws</display-name>
 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>com.jboss.webservice.Hello</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Hello</servlet-name>
  <url-pattern>/Hello</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>30</session-timeout>
 </session-config>

</web-app>

다른 방법으로는 EJB를 이용하여 port proxy를 remote stub을 이용하는 방법이 있지만 web이 테스트하기 보다 단순하므로 이 예제를 이용하겠습니다.

3. 서버에 디플로이 후 Web Service에 대한 deploy상태를 확인합니다.

http://localhost:8080/jbossws/ 라고 입력하면 웹서비스 리스트를 확인할 수 있습니다.


위의 화면에서 "View a list of deployed services"를 클릭하면 웹서비스들의 상태가 나타나게 됩니다.


4. WSDL의 디플로이된 상태를 확인합니다.


5. 이제 클라이언트 코드를 작성합니다. 여기서는 DII방식(Dynamic Invocation Interface) 방식을 이용하여 작성합니다.

package com.jboss.webservice.client;

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Call;

import javax.xml.namespace.QName;

import java.net.URL;

public class HelloClientDII
{
    public static void main(String[] args)
        throws Exception
    {
        String urlstr   = "http://localhost:8080/jboss-ws/Hello?wsdl";
        String argument = "Ji-Woong";

        System.out.println("Contacting webservice at " + urlstr);

        URL url =  new URL(urlstr);

        String ns        = "http://client.jboss.com/";
        QName  qname     = new QName(ns, "GreetService");
        QName  port      = new QName(ns, "HelloPort");
        QName  operation = new QName(ns, "greet");

        ServiceFactory factory = ServiceFactory.newInstance();
        Service        service = factory.createService(url, qname);
        Call           call    = service.createCall(port, operation);

        System.out.println("hello.hello(" + argument + ")");
        System.out.println("output:" + call.invoke(new Object[] {argument}));
    }
}

위의 코드를 실행하면 서버에 System console에 적은 내용이 찍히게 되며, 클라이언트에서 응답을 받을 수 있습니다.


다시 한번 "http://localhost:8080/jbossws/services" url을 확인하면 수행된 시간에 대한 결과치를 확인할 수 있습니다.
|
JDK 1.5이상부터 제공하는 Sun의 JConsole을 사용하면 VM에 대한 정보를 모니터링 할 수 있습니다. 이를 이용하여 또한 JBoss의 구동중인 VM상태를 모니터링 할 수 있습니다. 자세한 정보는 아래의 URL에서 확인하실 수 있습니다.
http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html
Technical Articles : http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

1. Local에서 구동중인 JBoss Server에 설정하기
JMX Agent를 Local의 JBoss 서버에 구동시키려면 다음의  java option을 run shell(run.conf/run.sh or run.bat)에 포함시켜 주시면 됩니다.

#
# Specify options to pass to the Java VM.
#
if [ "x$JAVA_OPTS" = "x" ]; then
   JAVA_OPTS="-server -Xms128m -Xmx128m"
fi

# Enable the jconsole agent locally
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote"

그런 다음 $JAVA_HOME/bin/jconsole을 실행시키면 다음의 화면이 나타납니다.
[jchoi@/opt/java1.5]jconsole

연결이 되면 아래의 그림처럼 VM 상황에 대한 자세한 정보를 확인할 수 있습니다.



2. Remote Server에서 구동중인 JBoss Server에 설정하기
Remote라고 해서 별다른 설정이 있는 게 아닙니다. 원격에서 실행하는 JBoss Server의 옵션에 필요한 파라미터를 전달받아 JConsole을 로컬에서 실행해서 보면 됩니다.

기본적으로는 다음의 스크립트를 추가하도록 합니다.
#
# Specify options to pass to the Java VM.
#
if [ "x$JAVA_OPTS" = "x" ]; then
   JAVA_OPTS="-server -Xms128m -Xmx128m"
fi
# Enable the jconsole agent remotely on port 12345
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=12345"



만약 security 옵션을 사용하지 않고 모니터링하고 싶다는 다음과 같이 start shell을 꾸며주면 됩니다.
# Enable the jconsole agent remotely on port 8888
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=8888"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"

위의 옵션을 준 후 jconsole을 실행시켜 remote탭을 보게 되면 연결할 수 있습니다.

위의 설정을 이용하여 추이를 지켜본 후 Memory Leak등의 여부를 그래프로 확인해볼 수 있을 겁니다.

|

놀새~'s Blog is powered by Daum & tistory