一个简单的Apollo程序(附源码)
06月 1st, 2007 — Dreamer在 EverythingFlex 那里看到了一个简单的Apollo程序,将Hibernate 与 Apollo 结合了起来,后台使用Hibernate 实现对数据库的操作,前台使用Apollo做UI。
原文地址:http://blog.everythingflex.com/2007/05/30/artemishibernate-apollo-application/
Employee.hbm.xml:
<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<!– Generated May 9, 2007 7:38:47 AM by Hibernate Tools 3.2.0.beta8 –>
<hibernate-mapping package=”com.everythingflex.artemisEmployee.vo” default-lazy=”false”>
<class name=”Employee” table=”Employee”>
<id name=”employeeId” type=”int”>
<column name=”EMPLOYEE_ID” />
<generator class=”assigned” />
</id>
<property name=”firstName” type=”string”>
<column name=”FIRST_NAME” length=”50″ not-null=”true” />
</property>
<property name=”lastName” type=”string”>
<column name=”LAST_NAME” length=”50″ not-null=”true” />
</property>
<property name=”email” type=”string”>
<column name=”EMAIL” length=”50″ not-null=”true” />
</property>
<property name=”jobTitle” type=”string”>
<column name=”JOB_TITLE” length=”50″ not-null=”true” />
</property>
</class>
</hibernate-mapping>
Employee.java bean:
package com.everythingflex.artemisEmployee.vo;public class Employee {
private int employeeId;
private String firstName;
private String lastName;
private String email;
private String jobTitle;
/**
*
*/
public Employee() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param employeeId
* @param firstName
* @param lastName
* @param email
* @param jobTitle
*/
public Employee(int employeeId, String firstName, String lastName, String email, String jobTitle) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.jobTitle = jobTitle;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the employeeId
*/
public int getEmployeeId() {
return employeeId;
}
/**
* @param employeeId the employeeId to set
*/
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the jobTitle
*/
public String getJobTitle() {
return jobTitle;
}
/**
* @param position the jobTitle to set
*/
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
}
Delegate.java (用来连接Apollo和JAVA):
package com.everythingflex.artemisEmployee.business;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.effectiveui.artemis.ArtemisBridge;
import com.effectiveui.artemis.event.ArtemisResultEvent;
import com.everythingflex.artemisEmployee.vo.Employee;
public class Delegate {
private SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
public ArtemisResultEvent getEmployee(Integer id){
ArtemisResultEvent result = new ArtemisResultEvent();
Session session = null;
try{;
session = sessionFactory.openSession();
Employee e = (Employee) session.load(Employee.class,id);
result.setResult(e);
} catch (Exception E) {
System.out.print(E);
} finally {
session.flush();
session.close();
}
return result;
}
public ArtemisResultEvent updateEmployee(Employee employee){
ArtemisResultEvent result = new ArtemisResultEvent();
Session session = null;
Transaction tx = null;
Boolean results = true;
try{
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.update(employee);
tx.commit();
} catch (Exception E) {
results = false;
System.out.print(E);
} finally {
tx = null;
session.flush();
session.close();
}
result.setResult(results);
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArtemisBridge.getInstance().bootstrap();
//Delegate d = new Delegate();
//d.getEmployee(1);
}
}
Employee.as :
package com.everythingflex.artemisEmployee.vo
{
[RemoteClass(alias="com.everythingflex.artemisEmployee.vo.Employee")]
[Bindable]
public class Employee
{ public var employeeId:int;
public var firstName:String;
public var lastName:String;
public var email:String;
public var jobTitle:String;
}
}
EmployeeArtemisObject.as (用来连接Apollo 和 JAVA):
package com.everythingflex.artemisEmployee.bridge
{
import com.effectiveui.artemis.mirror.ArtemisObject;
import com.effectiveui.artemis.mirror.call.ArtemisObjectCall;
import flash.utils.getQualifiedClassName;
import mx.rpc.events.ResultEvent;
import flash.events.EventDispatcher;
import com.everythingflex.artemisEmployee.vo.Employee; public class EmployeeArtemisObject extends ArtemisObject
{
public static const LIB_ID:String = “com.everythingflex.artemisEmployee.business.Delegate”;
public function EmployeeArtemisObject(){
super();
}
public function getEmployee(employeeId:int):void{
var artemisCall:ArtemisObjectCall = new ArtemisObjectCall(LIB_ID, “getEmployee”, employeeId);
artemisCall.addEventListener(ResultEvent.RESULT, getEmployeeResult);
send(artemisCall);
}
public function getEmployeeResult(e:ResultEvent):void{
EventDispatcher(e.target).removeEventListener(e.type, getEmployeeResult);
var rEvent:ResultEvent = new ResultEvent(”employeeResult”, false, true, e.result);
dispatchEvent(rEvent);
}
public function updateEmployee(employee:Employee):void{
var artemisCall:ArtemisObjectCall = new ArtemisObjectCall(LIB_ID, “updateEmployee”, employee);
artemisCall.addEventListener(ResultEvent.RESULT, updateEmployeeResult);
send(artemisCall);
}
public function updateEmployeeResult(e:ResultEvent):void{
EventDispatcher(e.target).removeEventListener(e.type, updateEmployeeResult);
var rEvent:ResultEvent = new ResultEvent(”updateEmployeeResult”, false, true, e.result);
dispatchEvent(rEvent);
}
}
}
Apollo主程序:
<?xml version=”1.0″ encoding=”utf-8″?><mx:ApolloApplication xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns:custom=”custom.*”
creationComplete=”initArtemisBridge()”
backgroundImage=”@Embed(’bg.png’)”
backgroundColor=”#FFFFFF” height=”420″ width=”340″ layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import com.effectiveui.artemis.mirror.call.ArtemisObjectCall;
import mx.rpc.events.ResultEvent;
import com.effectiveui.artemis.event.ArtemisServiceEvent;
import com.effectiveui.artemis.ArtemisBridge;
import com.everythingflex.artemisEmployee.vo.Employee;
import com.everythingflex.artemisEmployee.bridge.EmployeeArtemisObject;
private var bridge:ArtemisBridge = ArtemisBridge.getInstance();
private var employeeArtemisObject:EmployeeArtemisObject;
[Bindable]
private var isBridgeStarted:Boolean = false;
[Bindable]
private var updateEnabled:Boolean = false;
[Bindable]
private var employee:Employee;
private function initArtemisBridge():void{
ti.text = “Bridge Not Running \n”;
bridge.startServer();
bridge.addEventListener(ArtemisServiceEvent.ARTEMIS_STARTED, bridgeStarted);
employeeArtemisObject = new EmployeeArtemisObject();
employeeArtemisObject.addEventListener(”employeeResult”, employeeResult);
employeeArtemisObject.addEventListener(”updateEmployeeResult”, updateEmployeeResult);
}
private function bridgeStarted(e:ArtemisServiceEvent):void{
ti.text += “Bridge Started \n”;
isBridgeStarted = true;
}
private function getEmployee():void{
ti.text += “getEmployee(” + employeeId.value + “) \n”;
ti.verticalScrollPosition += 50;
employeeArtemisObject.getEmployee(employeeId.value);
}
private function employeeResult(_employee:ResultEvent):void{
employee = Employee(_employee.result);
if(employee.employeeId > 0){
updateEnabled = true;
}
}
private function updateEmployee(employee:Employee):void{
ti.text += “updateEmployee(” + employee + “) \n”;
ti.verticalScrollPosition += 75;
employee.firstName = firstName.text;
employee.lastName = lastName.text;
employee.email = email.text;
employee.jobTitle = jobTitle.text;
employeeArtemisObject.updateEmployee(employee);
}
private function updateEmployeeResult(status:ResultEvent):void{
if(status.result){
ti.text += “update successful \n”;
} else {
ti.text += “update failed \n”;
}
}
]]>
</mx:Script>
<mx:Style>
TextArea {
backgroundAlpha: .2;
color: #FFFFFF;
}
TextInput {
backgroundAlpha: .2;
color: #FFFFFF;
}
Label {
color: #FFFFFF;
}
</mx:Style>
<mx:TextArea id=”ti” width=”300″ height=”189″ x=”19″ y=”33″/>
<mx:NumericStepper minimum=”1″ maximum=”9″ id=”employeeId” stepSize=”1″ x=”151.5″ y=”230″ enabled=”{isBridgeStarted}” width=”45″/>
<mx:Button click=”getEmployee()” label=”Load Employee” x=”207″ y=”230″ enabled=”{isBridgeStarted}”/>
<mx:Label x=”19″ y=”232″ text=”Search by EmpoyeeId:”/>
<mx:Label x=”19″ y=”260″ text=”First Name:”/>
<mx:Label x=”19″ y=”286″ text=”Last Name:”/>
<mx:Label x=”19″ y=”312″ text=”Email:”/>
<mx:Label x=”19″ y=”340″ text=”Job Title:”/>
<mx:TextInput x=”89″ y=”284″ id=”lastName” text=”{employee.lastName}” width=”230″/>
<mx:TextInput x=”89″ y=”258″ id=”firstName” text=”{employee.firstName}” width=”230″/>
<mx:TextInput x=”89″ y=”310″ id=”email” text=”{employee.email}” width=”230″/>
<mx:TextInput x=”89″ y=”338″ id=”jobTitle” text=”{employee.jobTitle}” width=”230″/>
<mx:Label x=”19″ y=”15″ text=”Status Log”/>
<mx:Button x=”257″ y=”378″ label=”Update” width=”62″ click=”updateEmployee(employee)” enabled=”{updateEnabled}”/>
</mx:ApolloApplication>
程序截图:

本文链接: http://www.zhuoqun.net/html/y2007/564.html 转载请注明出处,谢谢。
TrackBack引用地址:http://www.zhuoqun.net/html/y2007/564.html/trackback











How can I close comunication between AIR application (client) and framework java. The problem is that if I close my application, server try to find out the client and CPU goes to 100%…..