본문 바로가기

spring

Spring 원격작업 기술

스프링은 다양한 원격작업 기술들을 제공하고 있습니다. 다 테스트하지는 못하고 몇개만 테스트 해보겠습니다.


참고 페이지는 다음과 같습니다.

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#remoting


STS에서 New Spring Legacy Project로 2개 프로젝트 2개 만들고 테스트용으로 작성한 소스는 아래와 같습니다.


아래 자바소스들은 서버, 클라이언트 양쪽에 있어야겠죠...


AccountService

package com.tistory.aircook.service;

 

import java.util.List;

 

import com.tistory.aircook.domain.Account;

 

public interface AccountService {

 

        public void insertAccount(Account account);

 

        public List<Account> getAccounts(String name);

}


AccountServiceImpl

package com.tistory.aircook.service;

 

import java.util.ArrayList;

import java.util.List;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import com.tistory.aircook.domain.Account;

 

@WebService(serviceName="AccountService")

public class AccountServiceImpl implements AccountService {

       

        private static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

       

        @WebMethod

        public void insertAccount(Account acc) {

              

               logger.info("insertAccount");

        }

        @WebMethod

        public List<Account> getAccounts(String name) {

              

               logger.info("getAccounts name : {}", name);

              

               List<Account> list = new ArrayList<Account>();

              

               Account account = new Account();

               account.setName("lee");

               list.add(account);

 

               account = new Account();

               account.setName("kim");

               list.add(account);

              

              

               for (Account ac : list) {

                       logger.debug("server name : {}", ac.getName());

               }

              

               return list;

        }

}


Account

package com.tistory.aircook.domain;

 

import java.io.Serializable;

 

public class Account implements Serializable {

 

        private String name;

 

        public String getName() {

               return name;

        }

 

        public void setName(String name) {

               this.name = name;

        }

}


spring-context.xml (서버)

<beans:bean id="accountService" class="com.tistory.aircook.service.AccountServiceImpl">

</beans:bean>

  

<!-- Add RMI ServiceExporter -->

<!-- RMI(Remote Method Invocation) 방화벽과 같은 네트워크 제약이 없는 상황에서 자바 기반의 서비스를 공개하거나 접근하려는 경우에 사용한다. -->

<beans:bean class="org.springframework.remoting.rmi.RmiServiceExporter">

    <beans:property name="serviceName" value="AccountService" />

    <beans:property name="service" ref="accountService" />

    <beans:property name="serviceInterface" value="com.tistory.aircook.service.AccountService" />

    <!-- defaults to 1099 -->

    <beans:property name="registryPort" value="1099" />

    <beans:property name="alwaysCreateRegistry" value="true" />

</beans:bean> 

 

<!-- Http Invoker ServiceExporter -->

<!-- HTTP Invoker 방화벽과 같이 네트워크 제약이 있는 상황에서 Spring 기반의 서비스를 공개하거나 접근하려는 경우에 사용한다. 이건 서버, 클라이언트 둘다 스프링! -->

<!-- <beans:bean id="httpInvokerServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> -->

<beans:bean id="httpInvokerServiceExporter" class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter">

    <beans:property name="service" ref="accountService"/>

    <beans:property name="serviceInterface" value="com.tistory.aircook.service.AccountService" />

</beans:bean>

 

<beans:bean id="httpServer"  class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">

        <beans:property name="contexts">

               <util:map>

                       <beans:entry key="/AccountService" value-ref="httpInvokerServiceExporter"/>

               </util:map>

        </beans:property>

        <beans:property name="port" value="7777" />

</beans:bean> 

 

<!-- JAX WS ServiceExporter -->

<!-- XML Web Service, CXF 사용안하고 이렇게도 된다 -->

<beans:bean id="jaxWsServiceExporter" class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">

        <beans:property name="baseAddress" value="http://localhost:8888/" />

</beans:bean>

 


spring-context.xml (클라이언트)

<beans:bean id="rmiAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

  <beans:property name="serviceUrl" value="rmi://***.***.***.***:1099/AccountService"/>

  <beans:property name="serviceInterface" value="com.tistory.aircook.service.AccountService"/>

  <beans:property name="lookupStubOnStartup" value="false"/>

</beans:bean> 

 

<beans:bean id="httpInvokerAccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

    <beans:property name="serviceUrl" value="http://***.***.***.***:7777/AccountService"/>

    <beans:property name="serviceInterface" value="com.tistory.aircook.service.AccountService"/>

     <!--

    <beans:property name="httpInvokerRequestExecutor">

        <beans:bean class="org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor">

                       <beans:property name="httpClient">

                       <beans:bean class="org.apache.commons.httpclient.HttpClient">

                  <beans:property name="connectionTimeout" value="2000"/>

                       </beans:bean>

               </beans:property>>

        </beans:bean>

    </beans:property>

     -->

</beans:bean>

 

<beans:bean id="jaxWsPortAccountService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">

        <beans:property name="serviceInterface" value="com.tistory.aircook.service.AccountService" />

        <beans:property name="wsdlDocumentUrl" value="http://localhost:8888/AccountService?wsdl" />

        <beans:property name="namespaceUri" value="http://service.aircook.tistory.com/" /> <!-- targetNamespace -->

        <beans:property name="serviceName" value="AccountService" /> <!-- service name -->

        <beans:property name="portName" value="AccountServiceImplPort" /> <!-- port name -->

</beans:bean>


TestController (클라이언트)

package com.tistory.aircook;

 

import java.text.DateFormat;

import java.util.Date;

import java.util.List;

import java.util.Locale;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

import com.tistory.aircook.domain.Account;

import com.tistory.aircook.service.AccountService;

 

/**

 * Handles requests for the application home page.

 */

@Controller

public class TestController {

       

        private static final Logger logger = LoggerFactory.getLogger(TestController.class);

       

        @Autowired

        @Qualifier("rmiAccountService")

        private AccountService rmiAccountService;

 

        @Autowired

        @Qualifier("httpInvokerAccountService")

        private AccountService httpInvokerAccountService;

       


        @Autowired

        @Qualifier("jaxWsPortAccountService")

        private AccountService jaxWsPortAccountService;

       

        /**

         * Simply selects the home view to render by returning its name.

         */

        @RequestMapping(value = "/", method = RequestMethod.GET)

        public String home(Locale locale, Model model) {

              

               logger.info("[http Invoker] -----------------------------------------------------------------");

               List<Account> httpInvokerList = httpInvokerAccountService.getAccounts("send httpInvoker");

              

               logger.info("client [http Invoker] getAccounts list :  {}.", httpInvokerList);

               for (Account ac : httpInvokerList) {

                       logger.debug("client [http Invoker] name : {}", ac.getName());

               }

 

               logger.info("[rmi] --------------------------------------------------------------------------");

               List<Account> rmiList = rmiAccountService.getAccounts("send rmi");

              

               logger.info("client [rmi] getAccounts list :  {}.", rmiList);

               for (Account ac : rmiList) {

                       logger.debug("client [rmi] name : {}", ac.getName());

               }

              

               logger.info("[jax-ws] -----------------------------------------------------------------------");

               List<Account> jaxWsList = jaxWsPortAccountService.getAccounts("send rmi");

              

               logger.info("client [jax-ws] getAccounts list :  {}.", jaxWsList);

               for (Account ac : jaxWsList) {

                       logger.debug("client [jax-ws] name : {}", ac.getName());

               }

              

               return "home";

        }

}