Add sample management server with loosely coupled sample components to test out the new RPC/messaging framework

This commit is contained in:
Kelven Yang 2012-12-12 18:02:05 -08:00
parent 11e9baca37
commit 75285f90ba
8 changed files with 371 additions and 7 deletions

View File

@ -32,14 +32,10 @@ import java.util.Set;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
import org.springframework.stereotype.Component;
// //
// Finding classes in a given package code is taken and modified from // Finding classes in a given package code is taken and modified from
// Credit: http://internna.blogspot.com/2007/11/java-5-retrieving-all-classes-from.html // Credit: http://internna.blogspot.com/2007/11/java-5-retrieving-all-classes-from.html
// //
@Component
public class OnwireClassRegistry { public class OnwireClassRegistry {
private List<String> packages = new ArrayList<String>(); private List<String> packages = new ArrayList<String>();
@ -48,6 +44,22 @@ public class OnwireClassRegistry {
public OnwireClassRegistry() { public OnwireClassRegistry() {
} }
public OnwireClassRegistry(String packageName) {
addPackage(packageName);
}
public OnwireClassRegistry(List<String> packages) {
packages.addAll(packages);
}
public List<String> getPackages() {
return packages;
}
public void setPackages(List<String> packages) {
this.packages = packages;
}
public void addPackage(String packageName) { public void addPackage(String packageName) {
packages.add(packageName); packages.add(packageName);
} }
@ -60,9 +72,11 @@ public class OnwireClassRegistry {
for(Class<?> clz : classes) { for(Class<?> clz : classes) {
OnwireName onwire = clz.getAnnotation(OnwireName.class); OnwireName onwire = clz.getAnnotation(OnwireName.class);
assert(onwire.name() != null); if(onwire != null) {
assert(onwire.name() != null);
registry.put(onwire.name(), clz); registry.put(onwire.name(), clz);
}
} }
} }

View File

@ -39,6 +39,10 @@ public class RpcProviderImpl implements RpcProvider {
public RpcProviderImpl() { public RpcProviderImpl() {
} }
public RpcProviderImpl(TransportProvider transportProvider) {
_transportProvider = transportProvider;
}
public TransportProvider getTransportProvider() { public TransportProvider getTransportProvider() {
return _transportProvider; return _transportProvider;
} }

View File

@ -0,0 +1,34 @@
/*
* 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.server;
import org.springframework.stereotype.Component;
@Component
public class SampleManagementServer {
public void mainLoop() {
while(true) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
}
}
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.server;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SampleManagementServerApp {
private static void setupLog4j() {
URL configUrl = System.class.getResource("/resources/log4j-cloud.xml");
if(configUrl != null) {
System.out.println("Configure log4j using log4j-cloud.xml");
try {
File file = new File(configUrl.toURI());
System.out.println("Log4j configuration from : " + file.getAbsolutePath());
DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000);
} catch (URISyntaxException e) {
System.out.println("Unable to convert log4j configuration Url to URI");
}
} else {
System.out.println("Configure log4j with default properties");
}
}
public static void main(String args[]) {
setupLog4j();
ApplicationContext context = new ClassPathXmlApplicationContext("/resources/SampleManagementServerAppContext.xml");
SampleManagementServer server = context.getBean(SampleManagementServer.class);
server.mainLoop();
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.server;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.framework.messaging.EventBus;
import org.apache.cloudstack.framework.messaging.EventDispatcher;
import org.apache.cloudstack.framework.messaging.EventHandler;
import org.apache.cloudstack.framework.messaging.RpcProvider;
import org.apache.cloudstack.framework.messaging.RpcServerCall;
import org.apache.cloudstack.framework.messaging.RpcServiceDispatcher;
import org.apache.cloudstack.framework.messaging.RpcServiceHandler;
import org.springframework.stereotype.Component;
@Component
public class SampleManagerComponent {
@Inject
private EventBus _eventBus;
@Inject
private RpcProvider _rpcProvider;
public SampleManagerComponent() {
}
@PostConstruct
public void init() {
_rpcProvider.registerRpcServiceEndpoint(
RpcServiceDispatcher.getDispatcher(this));
// subscribe to all network events (for example)
_eventBus.subscribe("network",
EventDispatcher.getDispatcher(this));
}
@RpcServiceHandler(command="NetworkPrepare")
void onStartCommand(RpcServerCall call) {
call.completeCall("NetworkPrepare completed");
}
@EventHandler(topic="network.prepare")
void onPrepareNetwork(String sender, String topic, Object args) {
}
}

View File

@ -0,0 +1,62 @@
/*
* 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.server;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.framework.messaging.EventBus;
import org.apache.cloudstack.framework.messaging.EventDispatcher;
import org.apache.cloudstack.framework.messaging.EventHandler;
import org.apache.cloudstack.framework.messaging.RpcProvider;
import org.apache.cloudstack.framework.messaging.RpcServerCall;
import org.apache.cloudstack.framework.messaging.RpcServiceDispatcher;
import org.apache.cloudstack.framework.messaging.RpcServiceHandler;
import org.springframework.stereotype.Component;
@Component
public class SampleManagerComponent2 {
@Inject
private EventBus _eventBus;
@Inject
private RpcProvider _rpcProvider;
public SampleManagerComponent2() {
}
@PostConstruct
public void init() {
_rpcProvider.registerRpcServiceEndpoint(
RpcServiceDispatcher.getDispatcher(this));
// subscribe to all network events (for example)
_eventBus.subscribe("storage",
EventDispatcher.getDispatcher(this));
}
@RpcServiceHandler(command="StoragePrepare")
void onStartCommand(RpcServerCall call) {
call.completeCall("StoragePrepare completed");
}
@EventHandler(topic="storage.prepare")
void onPrepareNetwork(String sender, String topic, Object args) {
}
}

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="org.apache.cloudstack, com.cloud" />
<bean id="transportProvider" class="org.apache.cloudstack.framework.messaging.server.ServerTransportProvider" init-method="initialize">
<property name="workerPoolSize" value="5" />
<property name="nodeId" value="Node1" />
</bean>
<bean id="rpcProvider" class="org.apache.cloudstack.framework.messaging.RpcProviderImpl" init-method="initialize">
<constructor-arg ref="transportProvider" />
</bean>
<bean id="eventBus" class = "org.apache.cloudstack.framework.messaging.EventBusBase" />
<bean id="onwireRegistry" class="org.apache.cloudstack.framework.messaging.OnwireClassRegistry"
init-method="scan" >
<property name="packages">
<list>
<value>org.apache.cloudstack.framework.messaging</value>
</list>
</property>
</bean>
</beans>

View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ================================= -->
<!-- Preserve messages in a local file -->
<!-- ================================= -->
<!-- A time/date based rolling appender -->
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="samplemanagementserver.log"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{3}] (%t:%x) %m%n"/>
</layout>
</appender>
<!-- ============================== -->
<!-- Append messages to the console -->
<!-- ============================== -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<!-- ================ -->
<!-- Limit categories -->
<!-- ================ -->
<category name="com.vmops.utils.db">
<priority value="DEBUG"/>
</category>
<category name="com.vmops.utils.db.Transaction.Transaction">
<priority value="TRACE"/>
</category>
<category name="com.vmops">
<priority value="TRACE"/>
</category>
<!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
<category name="org.apache">
<priority value="INFO"/>
</category>
<category name="org">
<priority value="INFO"/>
</category>
<category name="net">
<priority value="INFO"/>
</category>
<!-- ======================= -->
<!-- Setup the Root category -->
<!-- ======================= -->
<root>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>