Updated Branches: refs/heads/javelin 11e9baca3 -> 75285f90b
Add sample management server with loosely coupled sample components to test out the new RPC/messaging framework Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/75285f90 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/75285f90 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/75285f90 Branch: refs/heads/javelin Commit: 75285f90babd79c7471df58cfc03e24b2437ef0b Parents: 11e9bac Author: Kelven Yang <[email protected]> Authored: Wed Dec 12 18:02:05 2012 -0800 Committer: Kelven Yang <[email protected]> Committed: Wed Dec 12 18:02:05 2012 -0800 ---------------------------------------------------------------------- .../framework/messaging/OnwireClassRegistry.java | 28 ++++- .../framework/messaging/RpcProviderImpl.java | 4 + .../messaging/server/SampleManagementServer.java | 34 +++++ .../server/SampleManagementServerApp.java | 56 +++++++++ .../messaging/server/SampleManagerComponent.java | 63 ++++++++++ .../messaging/server/SampleManagerComponent2.java | 62 ++++++++++ .../resources/SampleManagementServerAppContext.xml | 37 ++++++ framework/ipc/test/resources/log4j-cloud.xml | 94 +++++++++++++++ 8 files changed, 371 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java ---------------------------------------------------------------------- diff --git a/framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java b/framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java index e787b60..d26777e 100644 --- a/framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java +++ b/framework/ipc/src/org/apache/cloudstack/framework/messaging/OnwireClassRegistry.java @@ -32,14 +32,10 @@ import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; -import org.springframework.stereotype.Component; - // // 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 // - -@Component public class OnwireClassRegistry { private List<String> packages = new ArrayList<String>(); @@ -48,6 +44,22 @@ public class 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) { packages.add(packageName); } @@ -60,9 +72,11 @@ public class OnwireClassRegistry { for(Class<?> clz : classes) { OnwireName onwire = clz.getAnnotation(OnwireName.class); - assert(onwire.name() != null); - - registry.put(onwire.name(), clz); + if(onwire != null) { + assert(onwire.name() != null); + + registry.put(onwire.name(), clz); + } } } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java ---------------------------------------------------------------------- diff --git a/framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java b/framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java index 409be59..9be50e5 100644 --- a/framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java +++ b/framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProviderImpl.java @@ -39,6 +39,10 @@ public class RpcProviderImpl implements RpcProvider { public RpcProviderImpl() { } + public RpcProviderImpl(TransportProvider transportProvider) { + _transportProvider = transportProvider; + } + public TransportProvider getTransportProvider() { return _transportProvider; } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServer.java ---------------------------------------------------------------------- diff --git a/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServer.java b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServer.java new file mode 100644 index 0000000..d00fba2 --- /dev/null +++ b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServer.java @@ -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) { + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServerApp.java ---------------------------------------------------------------------- diff --git a/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServerApp.java b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServerApp.java new file mode 100644 index 0000000..2748e7f --- /dev/null +++ b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagementServerApp.java @@ -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(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent.java ---------------------------------------------------------------------- diff --git a/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent.java b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent.java new file mode 100644 index 0000000..728f4ae --- /dev/null +++ b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent.java @@ -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) { + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent2.java ---------------------------------------------------------------------- diff --git a/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent2.java b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent2.java new file mode 100644 index 0000000..3dda9e2 --- /dev/null +++ b/framework/ipc/test/org/apache/cloudstack/framework/messaging/server/SampleManagerComponent2.java @@ -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) { + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/resources/SampleManagementServerAppContext.xml ---------------------------------------------------------------------- diff --git a/framework/ipc/test/resources/SampleManagementServerAppContext.xml b/framework/ipc/test/resources/SampleManagementServerAppContext.xml new file mode 100644 index 0000000..f53ced0 --- /dev/null +++ b/framework/ipc/test/resources/SampleManagementServerAppContext.xml @@ -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> http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/75285f90/framework/ipc/test/resources/log4j-cloud.xml ---------------------------------------------------------------------- diff --git a/framework/ipc/test/resources/log4j-cloud.xml b/framework/ipc/test/resources/log4j-cloud.xml new file mode 100644 index 0000000..e9b1918 --- /dev/null +++ b/framework/ipc/test/resources/log4j-cloud.xml @@ -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>
