Java 開發雲端手 (Restlet)
下午2:21 | Author: toBalaGe
有了 Ubuntu JeOS + 虛擬運算平台 (VMware) 這樣的雲端系統架構, 接下來的研究重點, 放在雲端系統中, 要使用那種開發架構, 來開發企業所需的應用系統. 最近有一個輕量開發技術 Java Restlet, 這技術滿類似 Client/Server 架構, 開發者不須了解滿複雜的 XML 通訊標準 (SOAP, UDDI,...)

程式實作
-----------
1. 下載 Restlet 開發套件與範例程式

1.2 版開發套件: http://www.restlet.org/downloads/
1.2 版範例程式 : http://www.restlet.org/documentation/1.2/examples/firstSteps/sources.zip

2. 解壓縮開發套件與範例程式

# unzip restlet-1.2m2.zip
# unzip sources.zip

3. 範例程式碼

HelloWorldResource.java
-------------------------------
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
* Resource which has only one representation.
*
*/
public class HelloWorldResource extends ServerResource {

@Get
public String represent() {
return "hello, world";
}

}

FirstStepsApplication.java
---------------------------------
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class FirstStepsApplication extends Application {

/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());

// Defines only one route
router.attachDefault(HelloWorldResource.class);

return router;
}

}

FirstStepsMain.java
--------------------------
import org.restlet.Component;
import org.restlet.data.Protocol;

public class FirstStepsMain {

public static void main(String[] args) {
try {
// Create a new Component.
Component component = new Component();

// Add a new HTTP server listening on port 8182.
component.getServers().add(Protocol.HTTP, 8182);

// Attach the sample application.
component.getDefaultHost().attach(new FirstStepsApplication());

// Start the component.
component.start();
} catch (Exception e) {
// Something is wrong.
e.printStackTrace();
}
}
}

範例程式說明 : http://www.restlet.org/documentation/1.2/firstSteps

4. 編譯程式

# javac -cp /opt/restlet-1.2m2/lib/org.restlet.jar *.java

5. 執行程式

# java -cp /opt/restlet-1.2m2/lib/org.restlet.jar TestFirstApp

6. 測試連接

在另一部電腦啟動 瀏覽器 (IE, Firefox,...), 輸入以下 URL

http://restletserver:8182
This entry was posted on 下午2:21 and is filed under , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.