STS 서버 배포 및 스케쥴러 사용
처음 자바를 할때 STS(Spring Tool Suite)로 시작을 했습니다. 뭐... 아무것도 모르다 보니 배포해야 할 폴더가 어디 있는지 무슨 파일을 배포 해야 하는지도 모르고 한참을 헤매였지요.
사실 지금도 모르는 부분이 많지만요...(특히 셋팅은 지금도 모르겠더라고요...;;;)
하여 오늘은 STS 서버 배포 및 스케쥴러 사용에 대한 두가지를 알아볼건데요.
첫번째로 STS 서버 배포 입니다.
C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
폴더에 가보시면 wtpwebapps라는 폴더가 있으실덴데요.
해당 프로젝트가 어느 tmp폴더에 있는지 모르니 하나하나 확인 후 wtpwebapps 폴더를 그냥 배포하시면 됩니다.
두번째로 Spring3 이후 부터 제공하는 어노테이션을 이용하여 간단한 로직의 반복 실행을 해주는 스케쥴러에 대해서 알아보겠습니다.
스케쥴러의 사용에는 Quartz라는 오픈소스 작업 스케쥴러가 있는데 로직이 복잡한 경우에 사용하는것으로 다음에 다루어보도록하고 오늘은 간단한 로직의 적용에 맞는 어노테이션을 이용하는 방법에 대해서 알아보겠습니다.
1. 스프링 설정 xml에 Spring Task 관련 속성 추가
--------------------
3) com.project.schedule 패키지 안에 Scheduler.java 생성
package com.project.schedule;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
@Component
public class Scheduler {
@Autowired
private SchedulerDao schedulerDao;
// @Scheduled 어노테이션은 리턴 타입이 void이고 파라미터를 갖지 않는 메서드에 적용되며
// 스케줄링 설정을 위해 cron : (cron = "5 * * * * *"), fixedRate(fixedRate = 5000), fixedDelay속성을 지정 가능.
@Scheduled(cron = "5 * * * * *")
public void cronTest1()
{
try {
String value = schedulerDao.test();
System.out.println("value:"+value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------
위 내용을 실행하시면 아래와 같은 실행 결과를 확인 하 실 수 있으십니다.
이상으로 STS 서버 배포 및 스케쥴러 사용에 대해 알아보았습니다.
오늘도 행복한 하루 되세요~~~^^
사실 지금도 모르는 부분이 많지만요...(특히 셋팅은 지금도 모르겠더라고요...;;;)
하여 오늘은 STS 서버 배포 및 스케쥴러 사용에 대한 두가지를 알아볼건데요.
첫번째로 STS 서버 배포 입니다.
C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
폴더에 가보시면 wtpwebapps라는 폴더가 있으실덴데요.
해당 프로젝트가 어느 tmp폴더에 있는지 모르니 하나하나 확인 후 wtpwebapps 폴더를 그냥 배포하시면 됩니다.
두번째로 Spring3 이후 부터 제공하는 어노테이션을 이용하여 간단한 로직의 반복 실행을 해주는 스케쥴러에 대해서 알아보겠습니다.
스케쥴러의 사용에는 Quartz라는 오픈소스 작업 스케쥴러가 있는데 로직이 복잡한 경우에 사용하는것으로 다음에 다루어보도록하고 오늘은 간단한 로직의 적용에 맞는 어노테이션을 이용하는 방법에 대해서 알아보겠습니다.
1. 스프링 설정 xml에 Spring Task 관련 속성 추가
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<task:annotation-driven />
요런식으로 beans 태그 하단 속성 추가
2. com.project.schedule 패키지 생성
3. SchedulerDaoImpl.java, SchedulerDao.java, Scheduler.java 생성
--------------------
1) com.project.schedule 패키지 안에 SchedulerDaoImpl.java 생성
package com.project.schedule;
import org.springframework.stereotype.Repository;
@Repository
public class SchedulerDaoImpl implements SchedulerDao {
public String test()
{
String str = "스케쥴 테스트";
return str;
}
}
--------------------
--------------------
2) com.project.schedule 패키지 안에 SchedulerDao.java 생성
package com.project.schedule;
public interface SchedulerDao {
public String test();
}
--------------------
--------------------
3) com.project.schedule 패키지 안에 Scheduler.java 생성
package com.project.schedule;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
@Component
public class Scheduler {
@Autowired
private SchedulerDao schedulerDao;
// @Scheduled 어노테이션은 리턴 타입이 void이고 파라미터를 갖지 않는 메서드에 적용되며
// 스케줄링 설정을 위해 cron : (cron = "5 * * * * *"), fixedRate(fixedRate = 5000), fixedDelay속성을 지정 가능.
@Scheduled(cron = "5 * * * * *")
public void cronTest1()
{
try {
String value = schedulerDao.test();
System.out.println("value:"+value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------
위 내용을 실행하시면 아래와 같은 실행 결과를 확인 하 실 수 있으십니다.
이상으로 STS 서버 배포 및 스케쥴러 사용에 대해 알아보았습니다.
오늘도 행복한 하루 되세요~~~^^
댓글
댓글 쓰기