캠핑과 개발

maven jetty plugin 옵션 중에 scanIntervalSeconds 라는 옵션을 줘서 파일이 변경됬을 경우 일정시간 마다 서버를 리스타트 하는 기능이 있다.

이것과 비슷하게 지정된 특정 디렉토리에 파일이나 디렉토리가 생성, 삭제, 변경 되는 것을 모니터링 하는 클래스를 맨들어 보았다.

이것을 위해서 Jetty util 패키지에 있는 Scanner 클래스를 사용했다.

maven 사용자는 다음과 같은 dependency 를 추가한다.

  1. <dependency>
  2.     <groupId>org.eclipse.jetty</groupId>
  3.     <artifactId>jetty-util</artifactId>
  4.     <version>8.0.1.v20110908</version>
  5. </dependency>


maven 사용안하는 사람은 jetty-util-8.0.1.v20110908.jar 요파일을 클래스 패스에 추가 시켜 주면 된다. 

jetty-util-8.0.1.v20110908.jar


FileChangeScanner 클래스는 다음과 같다. DiscreteListener 클래스의 각각의 경우에 대해서 원하는 엑션을 취해주면 된다.

  1. package scanner;
  2.  
  3. import java.io.File;
  4. import java.io.FileFilter;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9.  
  10. import org.eclipse.jetty.util.Scanner;
  11.  
  12. public class FileChangeScanner {
  13.     private Scanner scanner;
  14.  
  15.     public FileChangeScanner(String targetDir){
  16.         List<File> scanFiles = new ArrayList<File>();
  17.          
  18.         searchSubDirs(targetDir, scanFiles);
  19.          
  20.         scanner = new Scanner();
  21.         scanner.setScanInterval(1);          // 1초 간격으로 변경사항 스캔
  22.         scanner.setScanDirs(scanFiles);
  23.         scanner.setReportExistingFilesOnStartup(false);
  24.          
  25.         scanner.addListener(new Scanner.DiscreteListener() {
  26.             public void fileRemoved(String filename) throws Exception {
  27.                 System.out.println(filename + " is deleted");
  28.             }
  29.              
  30.             public void fileChanged(String filename) throws Exception {
  31.                 System.out.println(filename + " is changed");
  32.             }
  33.              
  34.             public void fileAdded(String filename) throws Exception {
  35.                 File f = new File(filename);
  36.                 if(f.isDirectory()) scanner.addScanDir(f);
  37.                  
  38.                 System.out.println(filename + " is added");
  39.             }
  40.         });
  41.     }
  42.      
  43.     public void start(){
  44.         try {
  45.             scanner.start();
  46.         } catch (Exception e) {
  47.             new RuntimeException(e);
  48.         }
  49.     }
  50.      
  51.     /**
  52.      * 하위 디렉토리들 찾기
  53.      * @param targetDir
  54.      * @param dirs
  55.      * @return
  56.      */
  57.     private List<File> searchSubDirs(String targetDir, final List<File> dirs){
  58.         File target = new File(targetDir);
  59.         target.listFiles(new FileFilter() {
  60.             public boolean accept(File file) {
  61.                 if( file.isDirectory() ) {
  62.                     dirs.add(file);
  63.                     searchSubDirs(file.toString(), dirs);
  64.                 }
  65.                 return false;
  66.             }
  67.         });
  68.          
  69.         return dirs;
  70.     }
  71.  
  72.     public static void main(String[] args) throws Exception{
  73.         // c:\test 하위 폴더 및 파일 변경 감시
  74.         new FileChangeScanner("c:\\test").start();
  75.          
  76.         // 실행하자 마자 프로그램이 종료되기 때문에 프로그램 종료방지를 위해 타이머 생성
  77.         // WAS 환경에서 돌릴때는 타이머 생성할 필요없음
  78.         Timer timer = new Timer();
  79.         timer.scheduleAtFixedRate(new TimerTask() {
  80.             public void run() {}
  81.         }60*100060*1000 );
  82.     }
  83. }


출처 : http://stove99.tistory.com/61

'DEVELOPMENT > Java' 카테고리의 다른 글

Ant-style path patterns  (0) 2019.02.07
spring @ControllerAdvice 설정 팁  (0) 2019.02.02
Java ImageIO 이미지 성능을위한 5 가지 팁  (0) 2017.01.18
<mvc:annotation-driven>이 하는 일  (0) 2016.07.28
java 파일 읽고 쓰기  (0) 2016.07.05

단순 참조일 경우

maven repository 가 없는 로컬 jar 파알을 maven 프로젝트에 추가 하기 위해서는 사설 repository를 만드는 방법도 있지만

다음과 같이 "dependency" 정의 시 scope 노드와 systemPath 노드를 사용하여 프로젝트에 포함된 jar 파일을 지정하여 줄 수 있다.

<dependency>
    <groupId>smack</groupId>
    <artifactId>smack-custom</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/smack-custom.jar</systemPath>
</dependency>

로컬을 Repository를 활용하는 방법

위와 같이 처리하는 경우는 문제점이 있다. scrop 의 system 이기 때문에 maven 빌드 시 해당 jar 파일이 포함되지 않는다.

이런 경우는 pom.xml 에 다음과 같이 정의하여 로컬을 repositoy로 활용하는 방법도 있다.

<dependency>
    <groupId>smack</groupId>
    <artifactId>smack-custom</artifactId>
    <version>1.0.0</version>
</dependency>
<repository>
	<id>in-project</id>
	<name>custom jars</name>
	<url>file://${project.basedir}/lib</url>
</repository>


이 때 ${project.basedir}/lib 는 maven 디렉토리 구조를 따르도록 구성해주어야 한다.

위의 예제에서는 디렉토리 및 파일명은 다음과 같이 구성해야 한다.

${project.basedir}/lib/smack/smack-custom/1.0.0/smack-custom-1.0.0.jar

만일 jenkins에서 maven 빌드를 하는 경우라면 다음과 같이 repository를 하나 더 추가해준다.

<repository>
	<id>in-project-jenkins</id>
	<name>custom jars-jenkins</name>
	<url>file://${JENKINS_HOME}/jobs/${JOB_NAME}/workspace/lib</url>
</repository>


출처 : http://itnp.kr/blog/post/Maven_Repository_%EA%B0%80_%EC%97%86%EB%8A%94_%EB%A1%9C%EC%BB%AC_jar_%ED%8C%8C%EC%9D%BC_%EC%9D%84_maven_project_%EC%97%90_%EC%B6%94%EA%B0%80%ED%95%98%EB%8A%94

'DEVELOPMENT' 카테고리의 다른 글

IDE 단축키  (0) 2018.11.28
hsqldb 사용하기  (0) 2016.07.23
windows에 있는 AppData 폴더란?  (0) 2015.07.30
Git  (0) 2013.12.30
dxf file format  (0) 2012.08.16

CentOS 7/RHEL 7부터는 방화벽을 데몬이 firewalld로 변경되었다. 방화벽 설정을 iptables 명령어 대신 firewall-cmd(콘솔), firewall-config(X-Windows) 명령어를 사용하여 설정한다. 


설치

  1. yum install firewalld
  2. systemctl start firewalld
  3. systemctl enable firewalld


설정

설정파일

/etc/firewalld/zones/public.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <zone>
  3.   <short>Public</short>
  4.   <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accept
  5. ed.</description>
  6.   <service name="dhcpv6-client"/>
  7.   <service name="http"/>
  8.   <service name="ssh"/>
  9.   <service name="https"/>
  10. </zone>


재기동

service iptables restart 대신 아래 명령어 사용

firewall-cmd --rel


zone

사전에 정의된 zone 목록 출력

firewall-cmd --get-zones


전체 존 목록을 상세하게 출력

firewall-cmd --list-all-zones


기존 존  출력

firewall-cmd --get-default-zone


활성화된 존 출력

firewall-cmd --get-active-zone


서비스 목록

firewall-cmd --get-services


permanent로 등록된 서비스 목록

firewall-cmd --permanent --list-all


임의 포트 추가

--add-port=<portid> [-<portid>]/<protocol> 옵션을 사용하여 포트 추가

firewall-cmd --zone=public --add-port=8080/tcp


포트 삭제

--remove-port=<portid> [-<portid>]/<protocol> 옵션 사용

firewall-cmd --zone=public --remove-port=8080/tcp


rich-rule

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4"  source address="192.168.10.0/24"  port protocol="tcp" port="9000" accept"


방화벽에 포트 추가

  1. firewall-cmd --permanent --zone=public --add-service=http
  2. firewall-cmd --permanent --zone=public --add-service=https

기본 zone은 public이므로 --zone=public 옵션은 생략 가능



출처 : https://www.lesstif.com/pages/viewpage.action?pageId=22053128#RHEL/CentOS7에서방화벽(firewalld)설정하기-설치



'DEVELOPMENT > Linux' 카테고리의 다른 글

logroate 옵션  (0) 2017.11.22
특정 계정으로 쉘 실행하기  (0) 2017.11.15
[Linux] Proxy 서버 설정하기  (0) 2015.07.22
[tip] 리눅스에서 부팅시 간단하기 시작 프로그램 등록하기  (0) 2015.05.12
crontab 설명  (0) 2015.05.06