Dec 28, 2010 - crontab -e 로 java를 실행하기

crontab -e 를 하게 되면 vi editor 상태로 들어가게 됩니다.

crontab 은 반복적으로 어떤 특정한 시간대에 어떤 작업을 시작하도록 예약할 필요성을 느낄때, 예약기능인 crontab 을 구현하여 저장할 수 있습니다.

crontab 은 다음과 같이

분  시 일 월 연 해당명령으로 구현되어있습니다.
30 16  *   *  *  echo "테스트"

매년 매월 매일 4시 30분에 “테스트”라는 값을 출력하도록 하는 명령어입니다.

이렇게 표준 출력으로 출력이 나오는 명령어는 특정 계정에게 메일로 전송됩니다. 이렇게 메일을 전송하지 않도록 하려면

00 06 * * * echo ‘I bug the system administrator daily at 6:00am!’ >/dev/null

###################################################
# TEST 망 RESTART SHELL SCRIPT
###################################################
20 4 * * 2 /xxx/restart.sh

crontab 안에 java class 를 직접 호출할 수도 있지만, 보통은 이렇게 sh 파일을 불려오는 데 crontab -e 를 사용합니다.

restart.sh 안에는 또다른 sh 파일을 넣어서 처리하도록 할 수 있습니다.

cd /xxx
stop.sh
start.sh

또한, 별도의 java class 를 호출하는 형태도 가능합니다.

#!/bin/ksh
# 통계 배치

BATCH_HOME=/xxx/batch

. ${BATCH_HOME}/SetEnv.sh $*

java test.xxxBatchProcess $1> ${BATCH_HOME}/log/xxxBatchProcess.log.`date +%y%m%d_%H%M%S`

이렇게 실제로 자바를 실행할 수도 있습니다. SetEnv.sh 에는 해당 자바 소스들을 import 합니다.

#!/bin/ksh

export LANG=ko
export PATH=/
export JAVA_HOME=/xxx/weblogic/bea/jdk150_06
export WEBAPP_PATH=/xxx/WEB-INF
export WEBAPP_LIB_PATH=$WEBAPP_PATH/lib
export CLASSPATH=.:$CLASSPATH:$WEBAPP_PATH/classes
export CLASSPATH=.:$CLASSPATH:$WEBAPP_LIB_PATH/commons-beanutils-1.7.0.jar

import 를 안해도 상관은 없지만, 그렇게 하면 java 의 속성값들에 해당 export 값들을 전부 넣어줘서 crontab 에 바로 등록도 가능합니다.

35 04 * * * /usr/local/java/bin/java -Xms516m -Xmx1024m -cp "/home/xxx/public_html/WEB-INF/classes:/home/xxx/public_html/WEB-INF/lib/*" com.adgather.frequency.log.xxx -DeleteBeforeData=Y -CheckAllDate=Y

여러 방법 중에 편한 방법이 가장 좋지만, 맨 앞에, restart.sh 처럼 crontab 에는 sh 파일을 불러오고, shell 에서 java or jar 를 불려와 처리하는 방안이 제일 무난하다고 생각합니다.

가끔 heap 메모리가 부족할 것이 걱정이 된다면, -Xms512m -Xmx2048m 와 같이 메모리 용량을 늘려주는게 좋을 거 같습니다.

#!/bin/ksh
# 통계 배치
BATCH_HOME=/xxx/batch
. ${BATCH_HOME}/SetEnv.sh $*
java -Xms512m -Xmx2048m test.xxxBatchProcess $1> ${BATCH_HOME}/log/xxxBatchProcess.log.`date +%y%m%d_%H%M%S`

이렇게 실제로 자바를 실행할 수도 있습니다.

ps. 위의 예제에서 > 는 해당 출력결과를 특정 log 파일에 저장합니다. 계속 같은 곳에 붙여넣기를 하는 경우도 있기 때문에 계속 날짜를 바꾸기 위한 date를 선언합니다. 만약 » 를 선언하여 계속 덮어 씌우는 것도 가능합니다. 그럴 경우에는 날짜를 바꾸는 date를 제외해야합니다.

Dec 28, 2010 - PoolLimitSQLException 관련 조치 사항

웹로직에서 PoolLimitSQLException 에러 대응 작업

“PoolLimitSQLException” 오류가 발생하였고, 이유는 웹로직(Weblogic)설정에 Maxinum capacity 가 15로 설정되어있었기 때문으로 추측됩니다.(기본 설정)

접속 Connection 이 많을때는 117까지 Idle이 증가되면서, 해당 오류가 발생된 것으로 파악되면서, 기본형으로 셋팅되었던 몇군대의 문제점이 발견되었습니다.

Home > Summary of Services: JDBC > Summary of JDBC Data Sources > XXX 설정

Test Connections On Reserve 를 Check 합니다. Check 할 경우 120 초 마다 “SELECT 1 FROM DUAL” 을 날려, 성공했을 경우 900초 동안은 검증이 되었다고 판단합니다.

900초는 너무 짧고, 1200 초가 적당하다고 합니다.

또한, 문제가 되었던 Maximum Capacity를 200으로 잡아주었습니다. 그런데 초기 용량이 1 이러다로 증가 수치 또한 1이면 부담이 될 수 있을 거 같습니다.

그 이유는, Monitoring 을 보면, 접속된 connections count 0인데, 가장 높은 count는 52 ~ 117까지 존재하는 걸 확인했고,
만약 초기용량이 1로 증가수치가 1씩 증가할 경우 connections 요청이 많을 경우 문제가 발생할 수 있을 걸로 추정되었습니다.

처음 용량을 5로 잡고 증가 수치 폭 또한 5씩 잡는 게 좋아 보아 같이 수정했습니다.

PS. Diagnostics 에서 Profile Connection Leak를 Check 를 해둬야할 거 같습니다. Monitoring 에서 해당 Leaked Count 를 알 수 있기 때문 있습니다. 이 옵션은 leaked connection이 connection pool에 자동 반환되는 것을 의미하는 것은 아니고, 단순히 로그에 leak정보의 출력을 의미하지만, Monitoring 에는 중요한 정보입니다

Dec 6, 2010 - Client IP 문제(X-Forwarded-For) 해결 방안

L4 에서 IP를 바꿔버리는 문제가 발생했습니다.
스위치를 예전 모델로 교체한다고 하여도 해당문제는 앞 단에 위치하게 되는 Firewall 장비에서 IP Remapping 할 가능성이 존재한다는 답변을 들었습니다.
L4 에서는 IP 를 Remapping 하면서 Client IP 를 아예 없애버리는 게 아니고 X-Forwarded-For 라는 헤더에 남겨 놓는다고 하여 해당 값을 가져다 쓰라고 하였습니다.
(대부분의 Proxy에서 이런 방법으로 IP를 기록한다고 합니다.)

기본적으로는 X-Forwarded-For 정보를 헤더에서 추출하고 그 값이 비어있을 경우 Remote-Addr 헤더로 문제없이 사용이 가능합니다.

참고로 해당 값은 LP VIP 을 통해 들어갈 때만 생기며, 클라이언트에서 보내주는 헤더값이 아니라 L4 VIP 를 통해 들어갈때 L4에서 헤더에 넣어주는 헤더값을 의미합니다.

자바내의 처리로직 :

    HttpServletRequest request

    String ip = request.getHeader("HTTP_X_FORWARDED_FOR");

    if(ip == null || ip.length() == 0 || ip.toLowerCase().equals("unknown"))
        ip = request.getHeader("REMOTE_ADDR");

    if(ip == null || ip.length() == 0 || ip.toLowerCase().equals("unknown"))
        ip = request.getRemoteAddr();

아래는 해외 문서를 참고한 내용입니다.

Getting The Real IP Of Your Users

There are a lot of sites around the internet that try and get your IP address. Most of their reasons are legitimate. For example Google Adsense will log your IP to see where the user clicking the advert is from (and kick you off if its the same IP you logged into that account with).

However, there are some sites that try to look at your IP for various reasons but do it wrong. Rapidshare is a beatifully painful example of this. If you’re on an ISP that uses a transparent proxy, RapidShare will log the proxy address instead of the actual account IP. As they limit the downloading on a per-IP basis, that means everyone using that ISP, going through that proxy, has the same IP to Rapidshare, meaning the limit to how much you download is split among those users.

What I’m saying here is, if you’re going to do your own IP lookups for whatever reason, do them correctly. My initial code here was in VB.net but since I have translated what its doing to the most popular server-side languages. As its based on the server variables, rather than the code’s process, its quite easy to port to something else if you need to.

The lookup that these “incorrect” sites are doing is something like this:

Request.ServerVariables("REMOTE_ADDR")
What then need to be doing is comparing the HTTP_X_FORWARDED_FOR variable against it, to check that there isn’t a non-transparent proxy in the way. Like so:

' Look for a proxy address firstDim _ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")' If there is no proxy, get the standard remote addressIf (_ip = "" Or _ip.ToLower = "unknown") Then _ _ip = Request.ServerVariables("REMOTE_ADDR")
This doesnt help people that are limited to (or otherwise) on anonymous proxies. They will hide the forwarding address (like they’re supposed to) and therefore the lookup will ONLY get the proxy’s address. Some ISPs do this by default to “protect” their users… Its just retarded. If you ISP does this, and you’ve been wondering why RS or other sites don’t work… Now you know.

If you want to check against an existing “wrong site”, try IP Chicken. It will return an incorrect value (eg the proxy). WhatsMyIP.orgis one that will look through the proxy and should give you the correct IP.

Here are some more examples in other languages:

C# —

// Look for a proxy address firstString
_ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
// If there is no proxy, get the standard remote address
If (_ip == "" || _ip.ToLower == "unknown")
_ip = Request.ServerVariables["REMOTE_ADDR"];

C# —

// Look for a proxy address firstString
_ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
// If there is no proxy, get the standard remote address
If (_ip == "" || _ip.ToLower == "unknown")
_ip = Request.ServerVariables["REMOTE_ADDR"];

PHP — Based on code from OxyScripts.

function GetUserIP() {
if (isset($_SERVER)) {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
return $_SERVER["HTTP_X_FORWARDED_FOR"];
if (isset($_SERVER["HTTP_CLIENT_IP"]))
return $_SERVER["HTTP_CLIENT_IP"];
return $_SERVER["REMOTE_ADDR"]; }
if (getenv('HTTP_X_FORWARDED_FOR'))
return getenv('HTTP_X_FORWARDED_FOR');
if (getenv('HTTP_CLIENT_IP'))
return getenv('HTTP_CLIENT_IP');
return getenv('REMOTE_ADDR');}

Java and JSP —

String ipaddress = request.getHeader("HTTP_X_FORWARDED_FOR");
if (ipaddress == null)
ipaddress = request.getRemoteAddr();

ASP/VBScript —

ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if ipaddress = ""
then
ipaddress = Request.ServerVariables("REMOTE_ADDR"
)end if

ColdFusion

<CFCOMPONENT>
<CFIF #CGI.HTTP_X_Forwarded_For# EQ "">
<CFSET ipaddress="#CGI.Remote_Addr#">
<CFELSE>
<CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
</CFIF>
</CFCOMPONENT>

Perl —

$ip = $req->header('Client-IP') || $req->header('Remote-Addr');
if ($req->header('X-Forwarded-For'))
{ $proxy = $ip; $ip = $req->header('X-Forwarded-For');}

If you know anymore, just ping them in my general direction and they can be added.

C# 계열은 정상 동작했는데, 우리쪽에서는 헤더정보를 읽어오지 못했습니다. 이유는 웹로직에서 포워딩할때 변경하는게 아닌가 추측됩니다. 모든 헤더 정보를 찍어보니 해당 헤더이름은 “X-Forwarded-For”(HTTP-X-FOWARDED-FOR) 로 변환해서 들어오는 것을 확인했습니다.

그것은 IIS 설정에서 X-Forwarded-For 를 IP로 잡는 겁니다. 인터넷에서 F5XForwardedFor 로 검색하면 상세한 정보를 알 수 있습니다.


# 참조 —–