2008. 9. 8. 17:00

[BlazeDS] RemoteObject 컴포넌트 / RemoteObject component

Reference : http://livedocs.adobe.com/blazeds/1/blazeds_devguide/rpc_remoteobject_1.html

RPC services > Using the Remoting Service > RemoteObject component

MXML 이나 ActionScript 에서 RemoteObject 컴포넌트를 선언하여 원격 서비스에 연결할 수 있습니다. Java 클래스나 ColdFusion 컴포넌트의 메쏘드를 호출하기 위해 RemoteObject 컴포넌트를 사용하십시오. 

Note: 이 문서는 Java 클래스에 연결하는 방법에 대해 설명하고 있습니다. ColdFusion 컴포넌트에 연결하는 정보는 ColdFusion 문서를 참조하십시오.

RemoteObject 컴포넌트의 destination 은 Remoting Service destination 의 source 로 정의된 Java 클래스 입니다. destination 정의는 원격 서비스의 집중화된 관리를 제공합니다. 그리고 destination 으로의 안전한 접근을 위해 기본 또는 사용자 정의 인증을 가능하게 합니다. destination 사이에서의 데이터 전송을 위해 여러 채널 중에서 하나를 선택할 수 있습니다. 그리고 서버에서 원격 서비스 트래픽의 로그를 남길 수 있습니다.

You can also use RemoteObject components with PHP and .NET objects in conjunction with third-party software, such as the open source projects AMFPHP and SabreAMF, and Midnight Coders WebORB. For more information, see the following websites:

  • AMFPHP http://amfphp.sourceforge.net/
  • SabreAMF http://www.osflash.org/sabreamf
  • Midnight Coders WebORB http://www.themidnightcoders.com/

Remoting Service channels
Remoting Service 채널

Remoting Service 에서 주로 AMFChannel 을 사용합니다. AMFChannel 은 HTTP 위에서 바이너리 AMF 인코딩을 사용합니다. 바이너리 데이터가 허용되지 않으면, HTTP 위에서 AMFX (AMF in XML) 를 사용하는 HTTPChannel 을 사용할 수 있습니다. 메시지 채널은 댁개 services-config.xml 파일에서 services-config 아래 channels 에 정의되어 있습니다. BlazeDS architecture 에서 채널에 대한 정보를 볼 수 있습니다.

Using a RemoteObject component
RemoteObject 컴포넌트 사용

아래의 예제는 destination 으로 연결하고, Button 컨트롤의 click 이벤트에 따라 데이터 소스로 요청을 보내며, TextArea 컨트롤의 text 속성에 리절트 데이터를 보여주는 RemoteObject 컴포넌트를 보여줍니다:

<?xml version="1.0"?> <!-- ds\rpc\RPCIntroExample1.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; public function handleResult(event:ResultEvent):void { // Handle result by populating the TextArea control. outputResult.text=remoteService.getData.lastResult.prop1; } public function handleFault(event:FaultEvent):void { // Handle fault. Alert.show(event.fault.faultString, "Fault"); } ]]> </mx:Script> <!-- Connect to a service destination.--> <mx:RemoteObject id="remoteService" destination="census" result="handleResult(event);" fault="handleFault(event);"/> <!-- Provide input data for calling the service. --> <mx:TextInput id="inputText"/> <!-- Call the web service, use the text in a TextInput control as input data.--> <mx:Button click="remoteService.getData(inputText.text)"/> <!-- Display results data in the user interface. --> <mx:TextArea id="outputResult"/> </mx:Application>

Defining remote Java objects
원격 Java 객체 정의

Remoting Service destination 이 HTTP 서비스 / 웹 서비스 destination 과 다른 점은 BlazeDS 웹 어플리케이션에서 원격 Java 객체를 호스트하고, destination 을 사용하여 참조하는 것입니다. With HTTP service and web service destinations, you typically configure the destination to access a remote service, external to the web application. 개발자는 Java 클래스를 작성하고 컴파일하여 웹 어플리케이션 클래스패스에 추가할 책임이 있습니다.

You can use any plain old Java object (POJO) that is available in the web application classpath as the source of the Remoting Service destination. The class must have a zero-argument constructor so that BlazeDS can construct an instance.

아래의 예제는 remoting-config.xml 파일에서의 Remoting Service destination 정의를 보여줍니다. source 는 웹 어플리케이션의 클래스패스에 있는 클래스의 fully qualified name 입니다.

<destination id="census"> <properties> <source>flex.samples.census.CensusService</source> </properties>
</destination>

아래의 예제는 destination 정의에서 참조되는 Java 클래스의 소스 코드를 보여줍니다:

package flex.samples.census; import java.util.ArrayList; import java.util.List; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import flex.samples.ConnectionHelper; public class CensusService { public List getElements(int begin, int count) { long startTime = System.currentTimeMillis(); Connection c = null; List list = new ArrayList(); String sql = "SELECT id, age, classofworker, education, maritalstatus, race, sex FROM census WHERE id > ? AND id <= ? ORDER BY id "; try { c = ConnectionHelper.getConnection(); PreparedStatement stmt = c.prepareStatement(sql); stmt.setInt(1, begin); stmt.setInt(2, begin + count); ResultSet rs = stmt.executeQuery(); while (rs.next()) { CensusEntryVO ce = new CensusEntryVO(); ce.setId(rs.getInt("id")); ce.setAge(rs.getInt("age")); ce.setClassOfWorker(rs.getString("classofworker")); ce.setEducation(rs.getString("education")); ce.setMaritalStatus(rs.getString("maritalstatus")); ce.setRace(rs.getString("race")); ce.setSex(rs.getString("sex")); list.add(ce); } } catch (SQLException e) { e.printStackTrace(); } finally { try { c.close(); } catch (Exception ignored) { } } return list; } }

Placing Java objects in the classpath
클래스패스에서 Java 객체 위치

The Remoting Service lets you access stateless and stateful objects that are in the classpath of the BlazeDS web application. Place class files in the WEB-INF\classes directory to add them to the classpath. Place Java Archive (JAR) files in the WEB-INF\lib directory to add them to the classpath.

Specify the fully qualified class name in the source property of a Remoting Service destination in the remoting-config.xml file. The class also must define a constructor that takes no arguments.

Converting ActionScript data to and from Java data
ActionScript 데이터와 Java 데이터 사이의 변환

Flex 어플리케이션에서 Java 객체로 데이터를 전달할 때, 데이터는 자동으로 ActionScript 데이터 타입에서 JAva 데이터 타입으로 변환됩니다. Java 메쏘드에서 리턴된 객체는 Java 에서 Action Script 로 변환됩니다. 데이터가 변환되는 방법은 Data serialization 에 자세하게 설명되어 있습니다.

Reserved method names for the RemoteObject component
RemoteObject 컴포넌트를 위한 예약된 메쏘드 이름

원격 메쏘드가 RemoteObject 클래스 나 그 상위 클래스에서 정의된 메쏘드와 같은 이름을 가지면, 원격 메쏘드를 직접 호출할 수 없습니다. RemoteObject 클래스는 아래의 메쏘드 이름을 정의하므로 Java 클래스에서 이 이름을 메쏘드 이름으로 사용하지 마십시오:

disconnect() getOperation() hasOwnProperty() initialized() isPrototypeOf() logout() propertyIsEnumerable() setCredentials() setPropertyIsEnumerable() setRemoteCredentials() toString() valueOf()

모든 예약된 메쏘드는 Adobe LiveCycle ES ActionScript Reference 에서 볼 수 있습니다. 또, Java 메쏘드 이름을 _ 로 시작하지 마십시오.

원격 메쏘드 이름이 예약된 메쏘드 이름과 같으면, 그 메쏘드를 나타내는 Operatior 객체를 리턴하는 RemoteObject 나 WebService 컴포넌트를 통해 아래의 ActionScript 메쏘드를 사용할 수 있습니다.:

public function getOperation(name:String):Operation

예를 들어, 리모트 메쏘드 이름이 hasOwnProperty() 라면, 아래의 예제와 같이 Operation 객체를 생성하십시오:

public var myRemoteObject:RemoteObject = new RemoteObject(); myRemoteObject.destination = "ro-catalog"; public var op:Operation = myRemoteObject.getOperation("hasOwnProperty");

아래의 예제에서 보이는 것처럼 Operation.send() 메쏘드를 사용하여 원격 메쏘드를 호출하십시오:

op.send();