Add compilable client RPC test file

This commit is contained in:
Kelven Yang 2012-12-04 10:42:45 -08:00
parent 2c689adce7
commit 1688919218
8 changed files with 98 additions and 8 deletions

View File

@ -29,14 +29,14 @@ public interface RpcClientCall {
Object getCommandArg();
RpcClientCall setContextParam(String key, Object param);
Object getContextParam(String key);
<T> T getContextParam(String key);
<T> RpcClientCall addCallbackListener(RpcCallbackListener<T> listener);
RpcClientCall setCallbackDispatcherTarget(Object target);
RpcClientCall setOneway();
void apply();
RpcClientCall apply();
void cancel();
/**

View File

@ -136,7 +136,7 @@ public class RpcClientCallImpl implements RpcClientCall {
}
@Override
public void apply() {
public RpcClientCall apply() {
// sanity check
assert(_sourceAddress != null);
assert(_targetAddress != null);
@ -152,6 +152,8 @@ public class RpcClientCallImpl implements RpcClientCall {
_rpcProvider.sendRpcPdu(getSourceAddress(), getTargetAddress(),
_rpcProvider.getMessageSerializer().serializeTo(RpcCallRequestPdu.class, pdu));
return this;
}
@Override

View File

@ -20,7 +20,7 @@ package org.apache.cloudstack.framework.messaging;
public interface RpcServerCall {
String getCommand();
Object getCommandArgument();
<T> T getCommandArgument();
// for receiver to response call
void completeCall(Object returnObject);

View File

@ -0,0 +1,28 @@
package org.apache.cloudstack.framework.messaging;
public class ClientOnlyEventDrivenStyle {
RpcProvider _rpcProvider;
public void AsyncCallRpcService() {
TestCommand cmd = new TestCommand();
_rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000)
.setCallbackDispatcherTarget(this)
.setContextParam("origCmd", cmd) // save context object for callback handler
.apply();
}
@RpcCallbackHandler(command="TestCommand")
public void OnAsyncCallRpcServiceCallback(RpcClientCall call) {
try {
TestCommand origCmd = call.getContextParam("origCmd"); // restore calling context at callback handler
TestCommandAnswer answer = call.get();
} catch(RpcTimeoutException e) {
} catch(RpcIOException e) {
} catch(RpcException e) {
}
}
}

View File

@ -0,0 +1,34 @@
package org.apache.cloudstack.framework.messaging;
public class ClientOnlyListenerStyle {
RpcProvider _rpcProvider;
public void AsyncCallRpcService() {
TestCommand cmd = new TestCommand();
_rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000)
.addCallbackListener(new RpcCallbackListener<TestCommandAnswer>() {
@Override
public void onSuccess(TestCommandAnswer result) {
}
@Override
public void onFailure(RpcException e) {
}
}).apply();
}
public void SyncCallRpcService() {
TestCommand cmd = new TestCommand();
RpcClientCall call = _rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000).apply();
try {
TestCommandAnswer answer = call.get();
} catch (RpcTimeoutException e) {
} catch (RpcIOException e) {
} catch (RpcException e) {
}
}
}

View File

@ -28,10 +28,6 @@ public class SampleComponent extends ComponentEndpoint {
call.completeCall("Call response");
}
@RpcCallbackHandler(command="StartCommand")
void onStartCommandCallback(RpcClientCall call) {
}
@EventHandler(topic="network.prepare")
void onPrepareNetwork(String sender, String topic, Object args) {
}

View File

@ -0,0 +1,7 @@
package org.apache.cloudstack.framework.messaging;
@OnwireName(name="TestCommand")
public class TestCommand {
public TestCommand() {
}
}

View File

@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.framework.messaging;
@OnwireName(name="TestCommandAnswer")
public class TestCommandAnswer {
}