Skip to content

Commit 1688919

Browse files
committed
Add compilable client RPC test file
1 parent 2c689ad commit 1688919

8 files changed

Lines changed: 98 additions & 8 deletions

File tree

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcClientCall.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public interface RpcClientCall {
2929
Object getCommandArg();
3030

3131
RpcClientCall setContextParam(String key, Object param);
32-
Object getContextParam(String key);
32+
<T> T getContextParam(String key);
3333

3434
<T> RpcClientCall addCallbackListener(RpcCallbackListener<T> listener);
3535
RpcClientCall setCallbackDispatcherTarget(Object target);
3636

3737
RpcClientCall setOneway();
3838

39-
void apply();
39+
RpcClientCall apply();
4040
void cancel();
4141

4242
/**

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcClientCallImpl.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void setCallTag(long callTag) {
136136
}
137137

138138
@Override
139-
public void apply() {
139+
public RpcClientCall apply() {
140140
// sanity check
141141
assert(_sourceAddress != null);
142142
assert(_targetAddress != null);
@@ -152,6 +152,8 @@ public void apply() {
152152

153153
_rpcProvider.sendRpcPdu(getSourceAddress(), getTargetAddress(),
154154
_rpcProvider.getMessageSerializer().serializeTo(RpcCallRequestPdu.class, pdu));
155+
156+
return this;
155157
}
156158

157159
@Override

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcServerCall.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
public interface RpcServerCall {
2222
String getCommand();
23-
Object getCommandArgument();
23+
<T> T getCommandArgument();
2424

2525
// for receiver to response call
2626
void completeCall(Object returnObject);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.apache.cloudstack.framework.messaging;
2+
3+
public class ClientOnlyEventDrivenStyle {
4+
RpcProvider _rpcProvider;
5+
6+
public void AsyncCallRpcService() {
7+
TestCommand cmd = new TestCommand();
8+
_rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000)
9+
.setCallbackDispatcherTarget(this)
10+
.setContextParam("origCmd", cmd) // save context object for callback handler
11+
.apply();
12+
}
13+
14+
@RpcCallbackHandler(command="TestCommand")
15+
public void OnAsyncCallRpcServiceCallback(RpcClientCall call) {
16+
try {
17+
TestCommand origCmd = call.getContextParam("origCmd"); // restore calling context at callback handler
18+
19+
TestCommandAnswer answer = call.get();
20+
21+
} catch(RpcTimeoutException e) {
22+
23+
} catch(RpcIOException e) {
24+
25+
} catch(RpcException e) {
26+
}
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.apache.cloudstack.framework.messaging;
2+
3+
public class ClientOnlyListenerStyle {
4+
5+
RpcProvider _rpcProvider;
6+
7+
public void AsyncCallRpcService() {
8+
TestCommand cmd = new TestCommand();
9+
_rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000)
10+
.addCallbackListener(new RpcCallbackListener<TestCommandAnswer>() {
11+
@Override
12+
public void onSuccess(TestCommandAnswer result) {
13+
}
14+
15+
@Override
16+
public void onFailure(RpcException e) {
17+
}
18+
}).apply();
19+
}
20+
21+
public void SyncCallRpcService() {
22+
TestCommand cmd = new TestCommand();
23+
RpcClientCall call = _rpcProvider.newCall("host-2").setCommand("TestCommand").setCommandArg(cmd).setTimeout(10000).apply();
24+
25+
try {
26+
TestCommandAnswer answer = call.get();
27+
} catch (RpcTimeoutException e) {
28+
29+
} catch (RpcIOException e) {
30+
31+
} catch (RpcException e) {
32+
}
33+
}
34+
}

‎framework/ipc/test/org/apache/cloudstack/framework/messaging/SampleComponent.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ void onStartCommand(RpcServerCall call) {
2828
call.completeCall("Call response");
2929
}
3030

31-
@RpcCallbackHandler(command="StartCommand")
32-
void onStartCommandCallback(RpcClientCall call) {
33-
}
34-
3531
@EventHandler(topic="network.prepare")
3632
void onPrepareNetwork(String sender, String topic, Object args) {
3733
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.cloudstack.framework.messaging;
2+
3+
@OnwireName(name="TestCommand")
4+
public class TestCommand {
5+
public TestCommand() {
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.framework.messaging;
20+
21+
@OnwireName(name="TestCommandAnswer")
22+
public class TestCommandAnswer {
23+
}

0 commit comments

Comments
 (0)