import net.customware.http.dispatch.shared.AbstractSimpleResult; public class PingActionResult extends AbstractSimpleResult<Object> { private static final long serialVersionUID = 1L; public PingActionResult(Object object) { super(object); } }
public class PingAction implements Action<PingActionResult> { private static final long serialVersionUID = 1L; private Object object; // private boolean generateException; // null private boolean nullResult; public PingAction(Object object) { this.object = object; } public PingAction(boolean nullResult, boolean generateException) { this.generateException = generateException; this.nullResult = nullResult; } public Object getObject() { return object; } public void setObject(Object object) { this.object = object; } public boolean isGenerateException() { return generateException; } public void setGenerateException(boolean generateException) { this.generateException = generateException; } public boolean isNullResult() { return nullResult; } public void setNullResult(boolean nullResult) { this.nullResult = nullResult; } }
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import net.customware.http.dispatch.server.ExecutionContext; import net.customware.http.dispatch.server.SimpleActionHandler; import net.customware.http.dispatch.shared.ActionException; import net.customware.http.dispatch.shared.DispatchException; import net.customware.http.dispatch.test.shared.PingAction; import net.customware.http.dispatch.test.shared.PingActionResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.inject.Inject; import com.google.inject.Provider; public class PingActionHandler extends SimpleActionHandler<PingAction, PingActionResult> { protected final Logger log = LoggerFactory.getLogger(getClass()); @Override public PingActionResult execute(PingAction action, ExecutionContext context) throws DispatchException { try { // if (action.isGenerateException()) { throw new Exception("Generated exception"); // null } else if (action.isNullResult()) { return null; // } else { Object object = action.getObject(); log.debug("Received object " + object); return new PingActionResult(object); } } catch (Exception cause) { log.error("Unable to perform ping action", cause); // , // . // ActionException, // http-dispatch throw new ActionException(cause); } } // rollback , // BatchAction. , // . @Override public void rollback(PingAction action, PingActionResult result, ExecutionContext context) throws DispatchException { log.debug("PingAction rollback called"); } }
import net.customware.http.dispatch.server.BatchActionHandler; import net.customware.http.dispatch.server.DefaultActionHandlerRegistry; import net.customware.http.dispatch.server.Dispatch; import net.customware.http.dispatch.server.InstanceActionHandlerRegistry; import net.customware.http.dispatch.server.SimpleDispatch; import net.customware.http.dispatch.server.standard.AbstractStandardDispatchServlet; import net.customware.http.dispatch.test.server.handler.PingActionHandler; /** */ public class StandardDispatcherTestService extends AbstractStandardDispatchServlet { private static final long serialVersionUID = 1L; private Dispatch dispatch; public StandardDispatcherTestService() { // Setup for test case InstanceActionHandlerRegistry registry = new DefaultActionHandlerRegistry(); registry.addHandler( new BatchActionHandler() ); registry.addHandler(new PingActionHandler()); dispatch = new SimpleDispatch( registry ); } @Override protected Dispatch getDispatch() { return dispatch; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>DispatchServlet</servlet-name> <servlet-class>net.customware.http.dispatch.test.server.standard.StandardDispatcherTestService</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatchServlet</servlet-name> <url-pattern>/standard_dispatch</url-pattern> </servlet-mapping> </web-app>
public <R extends Result> void execute( final Action<R> action, final AsyncCallback<R> callback) { // -"edt" "edt" final Handler uiThreadCallback = new Handler(); new Thread() { @Override public void run() { final Object result = getResult(action); // і "edt" , // final Runnable runInUIThread = new Runnable() { @Override public void run() { HttpUtils.processResult(result, callback); } }; uiThreadCallback.post(runInUIThread); } }.start(); }
DispatchAsync dispatch = new StandardDispatchAsync( new DefaultExceptionHandler(), new AndroidStandardDispatchServiceAsync(DISPATCH_URL_STANDARD));
AsyncCallback<PingRequestResult> callback = new AsyncCallback<PingRequestResult>()
public void onSuccess(PingRequestResult result)
public void onFailure(Throwable caught)
dispatcher.execute(pingRequest, callback);
import java.util.ArrayList; import java.util.List; import net.customware.http.dispatch.client.AsyncCallback; import net.customware.http.dispatch.client.DefaultExceptionHandler; import net.customware.http.dispatch.client.DispatchAsync; import net.customware.http.dispatch.client.android.AndroidSecureDispatchServiceAsync; import net.customware.http.dispatch.client.android.AndroidStandardDispatchServiceAsync; import net.customware.http.dispatch.client.guice.SecureDispatchModule; import net.customware.http.dispatch.client.secure.CookieSecureSessionAccessor; import net.customware.http.dispatch.client.standard.StandardDispatchAsync; import net.customware.http.dispatch.test.shared.PingRequest; import net.customware.http.dispatch.test.shared.PingRequestResult; public class RPCUtils { protected static final String DISPATCH_URL_STANDARD = "http://httpdispatch-ep.rhcloud.com/standard_dispatch"; static DispatchAsync dispatch = new StandardDispatchAsync( new DefaultExceptionHandler(), new AndroidStandardDispatchServiceAsync(DISPATCH_URL_STANDARD)); public static DispatchAsync getDispatchAsync() { return dispatch; } // public static void runBasicStringTest(LogWrapper log) { String testObject = "Test String Object"; PingRequest pingRequest = new PingRequest(testObject); testCommon(pingRequest, log); } // ArrayList<String> public static void runBasicListTest(LogWrapper log) { List<String> testList = new ArrayList<String>(); testList.add("one"); testList.add("two"); PingRequest pingRequest = new PingRequest(testList); testCommon(pingRequest, log); } // null public static void runNullSubObjectTest(LogWrapper log) { PingRequest pingRequest = new PingRequest(null); testCommon(pingRequest, log); } // null public static void runNullObjectTest(LogWrapper log) { PingRequest pingRequest = new PingRequest(true, false); testCommon(pingRequest, log); } // public static void runExceptionTest(LogWrapper log) { PingRequest pingRequest = new PingRequest(false, true); testCommon(pingRequest, log); } private static void testCommon(PingRequest pingRequest, final LogWrapper log) { final long start = System.currentTimeMillis(); log.log("Sending object: " + pingRequest.getObject()); DispatchAsync dispatcher = getDispatchAsync(); AsyncCallback<PingRequestResult> callback = new AsyncCallback<PingRequestResult>() { @Override public void onSuccess(PingRequestResult result) { if (result == null) { log.log("Received null at " + (System.currentTimeMillis() - start) + "ms"); } else { log.log("Received result: " + result.get() + " at " + (System.currentTimeMillis() - start) + "ms"); } } @Override public void onFailure(Throwable caught) { log.log("Received exception: " + caught.getMessage() + " at " + (System.currentTimeMillis() - start) + "ms"); } }; dispatcher.execute(pingRequest, callback); } }
public interface LogWrapper { void log(String text); }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/actionTypeSP" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /> <Button android:id="@+id/runBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/actionTypeSP" android:text="Run" /> <ScrollView android:id="@+id/scroller" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/runBtn" android:background="#FFFFFF" > <TextView android:id="@+id/logView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:scrollHorizontally="false" android:scrollbars="vertical" android:textSize="15sp" /> </ScrollView> </RelativeLayout>
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ScrollView; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { enum ActionType { BASIC_STRING_OBJECT("Basic Object Send/Receive"), BASIC_ARRAYLIST_OBJECT("Basic ArrayList Send/Receive"), NULL_SUB_OBJECT("Null argument Send/Receive"), NULL_OBJECT("Null Receive"), EXCEPTION("Remote Exception") ; String description; ActionType(String description) { this.description = description; } @Override public String toString() { return description; } } Spinner actionTypeSP; TextView logView; ScrollView scroller; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } void init() { initActionList(); logView = (TextView) findViewById(R.id.logView); scroller = (ScrollView) findViewById(R.id.scroller); initRunButton(); } void initActionList() { actionTypeSP = (Spinner) findViewById(R.id.actionTypeSP); ArrayAdapter<ActionType> dataAdapter = new ArrayAdapter<ActionType>( this, android.R.layout.simple_spinner_item, ActionType.values()); dataAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); actionTypeSP.setAdapter(dataAdapter); } void initRunButton() { Button runBtn = (Button) findViewById(R.id.runBtn); final LogWrapper log = new LogWrapper() { @Override public void log(String text) { MainActivity.this.log(text); } }; runBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ActionType actionType = (ActionType) actionTypeSP .getSelectedItem(); if (actionType != null) { switch (actionType) { case BASIC_STRING_OBJECT: RPCUtils.runBasicStringTest(log); break; case BASIC_ARRAYLIST_OBJECT: RPCUtils.runBasicListTest(log); break; case EXCEPTION: RPCUtils.runExceptionTest(log); break; case NULL_OBJECT: RPCUtils.runNullObjectTest(log); break; case NULL_SUB_OBJECT: RPCUtils.runNullSubObjectTest(log); break; default: break; } } } }); } void log(String str) { String text = logView.getText().toString(); text += str + "\n"; logView.setText(text); scroller.smoothScrollTo(0, logView.getBottom()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.customware.http.dispatch.test.client.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Source: https://habr.com/ru/post/148436/
All Articles