일단 먼저 lcds를 설치 해야 한다.
설치가 완료되면 해당 폴더(기본적으로 c:\lcds에 위치함)안에 flex.war파일이 있다.
이 파일을 복사해서 톰캣홈/webapps폴더(기본적으로 C:\Program Files\Apache Software
Foundation\Tomcat 5.5\webapps에 위치함) 안에 넣은 후에 톰캣을 재가동 시키면
flex폴더가 새로 생기게 된다.
flex폴더를 생성하게 되면 그 하위 폴더로 WEB-INF\flex가 있는데 이 안에 보면
services-config.xml 파일과 remoting-config.xml 파일이 있는데 이 두개의 파일을
설정함으로써 RemoteObject를 사용할 수 있게 된다.
먼저 services-config.xml 파일을 수정해야 한다.
channels 안에 아래와 같은 소스를 입력한다.
톰캣을 사용하기 때문에 8080을 써주고 id값은 임의로 줘도 되므로 test-amf라고 하였다.
- <channel-definition id="test-amf" class="mx.messaging.channels.AMFChannel">
- <endpoint url=
- "https://localhost:8080/flex/messagebroker/amf"
- class="flex.messaging.endpoints.AMFEndpoint"/>
- <properties>
- <polling-enabled>false</polling-enabled>
- </properties>
- </channel-definition>
이렇게 해준 후, my-amf 가 있는데 두개의 amf를 한꺼번에 사용할 수 없으므로 아래와 같이
my-amf에 있는 url의 amf뒤에 2라고 써준다.
https://localhost:8080/flex/messagebroker/amf2
이번엔 remoting-config.xml을 수정할 차례다.
remoting-config.xml에서는 사용할 java클래스를 등록하고 사용할 수 있도록 한다.
아래 소스에서 services-config.xml 파일에서 만들어준 test-amf를 등록해 주었다.
그리고 destination안의 id는 플렉스 상에서 지정하게 될 id값으로 사용된다.
source안에는 사용할 클래스 이름을 써주게 되고 channel ref는 보면 알겠지만
사용할 channel이름을 입력해 주면 된다.
- <default-channels>
- <channel ref="test-amf"/>
- </default-channels>
- <destination id="RO">
- <properties>
- <source>RemoteObject</source>
- </properties>
- <channels>
- <channel ref="test-amf" />
- </channels>
- </destination>
- <destination id="STUDENT">
- <properties>
- <source>StudentManager</source>
- </properties>
- <channels>
- <channel ref="test-amf" />
- </channels>
- </destination>
이와 같이 자바 클래스를 사용하고자 할때는 remoting-config.xml 파일에 등록하여
사용하면 되고 여기까지가 RemoteObject를 사용하기 위한 설정이였다.
이제부턴 실제로 프로그래밍을 통해 RemoteObject를 사용하면 된다.
java클래스파일을 만드는데 java파일을 컴파일한 후 클래스파일은 톰캣홈
\webapps\flex\WEB-INF\classes 안에 위치하게 된다.
테스트용으로 아래와 같이 java파일을 만들었다.
- public class RemoteObject
- {
- public String my_func()
- {
- return "Java 함수가 호출된다.";
- }
- }
RemoteObject라는 클래스 이름은 remotting-config.xml 파일에
<source>RemoteObject</source>와 같이 등록되어 있어야 사용이 가능하며 위에
이미 설정되어있다.
그리고 마지막으로 Flex 상에서는 아래와 같이 해당 클래스의 메소드를 호출 할 수 있다는
것을 알 수 있다.
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- private function resultHandler(event:ResultEvent):void
- {
- txtResult.text = event.result.toString();
- }
- private function faultHandler(event:FaultEvent):void
- {
- txtResult.text = event.toString();
- }
- ]]>
- </mx:Script>
- <mx:RemoteObject id="test" destination="RO" showBusyCursor="true"
- result="resultHandler(event)" fault="faultHandler(event)" >
- </mx:RemoteObject>
- <mx:Button x="19" y="10" label="RemoteObject호출" click="test.my_func()" />
- <mx:TextArea id="txtResult" x="10" y="40" width="229" height="218" />
- </mx:Application>