Skip to content

Commit 75285f9

Browse files
committed
Add sample management server with loosely coupled sample components to test out the new RPC/messaging framework
1 parent 11e9bac commit 75285f9

8 files changed

Lines changed: 371 additions & 7 deletions

File tree

framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@
3232
import java.util.jar.JarEntry;
3333
import java.util.jar.JarInputStream;
3434

35-
import org.springframework.stereotype.Component;
36-
3735
//
3836
// Finding classes in a given package code is taken and modified from
3937
// Credit: http://internna.blogspot.com/2007/11/java-5-retrieving-all-classes-from.html
4038
//
41-
42-
@Component
4339
public class OnwireClassRegistry {
4440

4541
private List<String> packages = new ArrayList<String>();
@@ -48,6 +44,22 @@ public class OnwireClassRegistry {
4844
public OnwireClassRegistry() {
4945
}
5046

47+
public OnwireClassRegistry(String packageName) {
48+
addPackage(packageName);
49+
}
50+
51+
public OnwireClassRegistry(List<String> packages) {
52+
packages.addAll(packages);
53+
}
54+
55+
public List<String> getPackages() {
56+
return packages;
57+
}
58+
59+
public void setPackages(List<String> packages) {
60+
this.packages = packages;
61+
}
62+
5163
public void addPackage(String packageName) {
5264
packages.add(packageName);
5365
}
@@ -60,9 +72,11 @@ public void scan() {
6072

6173
for(Class<?> clz : classes) {
6274
OnwireName onwire = clz.getAnnotation(OnwireName.class);
63-
assert(onwire.name() != null);
64-
65-
registry.put(onwire.name(), clz);
75+
if(onwire != null) {
76+
assert(onwire.name() != null);
77+
78+
registry.put(onwire.name(), clz);
79+
}
6680
}
6781
}
6882

framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class RpcProviderImpl implements RpcProvider {
3939
public RpcProviderImpl() {
4040
}
4141

42+
public RpcProviderImpl(TransportProvider transportProvider) {
43+
_transportProvider = transportProvider;
44+
}
45+
4246
public TransportProvider getTransportProvider() {
4347
return _transportProvider;
4448
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.server;
20+
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class SampleManagementServer {
25+
26+
public void mainLoop() {
27+
while(true) {
28+
try {
29+
Thread.currentThread().sleep(1000);
30+
} catch (InterruptedException e) {
31+
}
32+
}
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.server;
20+
21+
import java.io.File;
22+
import java.net.URISyntaxException;
23+
import java.net.URL;
24+
25+
import org.apache.log4j.xml.DOMConfigurator;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.context.support.ClassPathXmlApplicationContext;
28+
29+
public class SampleManagementServerApp {
30+
31+
private static void setupLog4j() {
32+
URL configUrl = System.class.getResource("/resources/log4j-cloud.xml");
33+
if(configUrl != null) {
34+
System.out.println("Configure log4j using log4j-cloud.xml");
35+
36+
try {
37+
File file = new File(configUrl.toURI());
38+
39+
System.out.println("Log4j configuration from : " + file.getAbsolutePath());
40+
DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000);
41+
} catch (URISyntaxException e) {
42+
System.out.println("Unable to convert log4j configuration Url to URI");
43+
}
44+
} else {
45+
System.out.println("Configure log4j with default properties");
46+
}
47+
}
48+
49+
public static void main(String args[]) {
50+
setupLog4j();
51+
52+
ApplicationContext context = new ClassPathXmlApplicationContext("/resources/SampleManagementServerAppContext.xml");
53+
SampleManagementServer server = context.getBean(SampleManagementServer.class);
54+
server.mainLoop();
55+
}
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.server;
20+
21+
import javax.annotation.PostConstruct;
22+
import javax.inject.Inject;
23+
24+
import org.apache.cloudstack.framework.messaging.EventBus;
25+
import org.apache.cloudstack.framework.messaging.EventDispatcher;
26+
import org.apache.cloudstack.framework.messaging.EventHandler;
27+
import org.apache.cloudstack.framework.messaging.RpcProvider;
28+
import org.apache.cloudstack.framework.messaging.RpcServerCall;
29+
import org.apache.cloudstack.framework.messaging.RpcServiceDispatcher;
30+
import org.apache.cloudstack.framework.messaging.RpcServiceHandler;
31+
import org.springframework.stereotype.Component;
32+
33+
@Component
34+
public class SampleManagerComponent {
35+
36+
@Inject
37+
private EventBus _eventBus;
38+
39+
@Inject
40+
private RpcProvider _rpcProvider;
41+
42+
public SampleManagerComponent() {
43+
}
44+
45+
@PostConstruct
46+
public void init() {
47+
_rpcProvider.registerRpcServiceEndpoint(
48+
RpcServiceDispatcher.getDispatcher(this));
49+
50+
// subscribe to all network events (for example)
51+
_eventBus.subscribe("network",
52+
EventDispatcher.getDispatcher(this));
53+
}
54+
55+
@RpcServiceHandler(command="NetworkPrepare")
56+
void onStartCommand(RpcServerCall call) {
57+
call.completeCall("NetworkPrepare completed");
58+
}
59+
60+
@EventHandler(topic="network.prepare")
61+
void onPrepareNetwork(String sender, String topic, Object args) {
62+
}
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.server;
20+
21+
import javax.annotation.PostConstruct;
22+
import javax.inject.Inject;
23+
24+
import org.apache.cloudstack.framework.messaging.EventBus;
25+
import org.apache.cloudstack.framework.messaging.EventDispatcher;
26+
import org.apache.cloudstack.framework.messaging.EventHandler;
27+
import org.apache.cloudstack.framework.messaging.RpcProvider;
28+
import org.apache.cloudstack.framework.messaging.RpcServerCall;
29+
import org.apache.cloudstack.framework.messaging.RpcServiceDispatcher;
30+
import org.apache.cloudstack.framework.messaging.RpcServiceHandler;
31+
import org.springframework.stereotype.Component;
32+
33+
@Component
34+
public class SampleManagerComponent2 {
35+
@Inject
36+
private EventBus _eventBus;
37+
38+
@Inject
39+
private RpcProvider _rpcProvider;
40+
41+
public SampleManagerComponent2() {
42+
}
43+
44+
@PostConstruct
45+
public void init() {
46+
_rpcProvider.registerRpcServiceEndpoint(
47+
RpcServiceDispatcher.getDispatcher(this));
48+
49+
// subscribe to all network events (for example)
50+
_eventBus.subscribe("storage",
51+
EventDispatcher.getDispatcher(this));
52+
}
53+
54+
@RpcServiceHandler(command="StoragePrepare")
55+
void onStartCommand(RpcServerCall call) {
56+
call.completeCall("StoragePrepare completed");
57+
}
58+
59+
@EventHandler(topic="storage.prepare")
60+
void onPrepareNetwork(String sender, String topic, Object args) {
61+
}
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:tx="http://www.springframework.org/schema/tx"
7+
xmlns:aop="http://www.springframework.org/schema/aop"
8+
xsi:schemaLocation="http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10+
http://www.springframework.org/schema/tx
11+
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12+
http://www.springframework.org/schema/aop
13+
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14+
http://www.springframework.org/schema/context
15+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16+
<context:annotation-config />
17+
<context:component-scan base-package="org.apache.cloudstack, com.cloud" />
18+
19+
<bean id="transportProvider" class="org.apache.cloudstack.framework.messaging.server.ServerTransportProvider" init-method="initialize">
20+
<property name="workerPoolSize" value="5" />
21+
<property name="nodeId" value="Node1" />
22+
</bean>
23+
<bean id="rpcProvider" class="org.apache.cloudstack.framework.messaging.RpcProviderImpl" init-method="initialize">
24+
<constructor-arg ref="transportProvider" />
25+
</bean>
26+
27+
<bean id="eventBus" class = "org.apache.cloudstack.framework.messaging.EventBusBase" />
28+
<bean id="onwireRegistry" class="org.apache.cloudstack.framework.messaging.OnwireClassRegistry"
29+
init-method="scan" >
30+
<property name="packages">
31+
<list>
32+
<value>org.apache.cloudstack.framework.messaging</value>
33+
</list>
34+
</property>
35+
</bean>
36+
37+
</beans>

0 commit comments

Comments
 (0)