add initial draft for hello world example
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.gemstone.gemfire.cache.EntryEvent;
|
||||
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
|
||||
|
||||
/**
|
||||
* Listener that logs entry operations to the configured logger.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class CacheLogger extends CacheListenerAdapter<Object, Object> {
|
||||
|
||||
private static final Log log = LogFactory.getLog(CacheLogger.class);
|
||||
|
||||
@Override
|
||||
public void afterCreate(EntryEvent<Object, Object> event) {
|
||||
log.info("Added " + messageLog(event) + " to the cache");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterDestroy(EntryEvent<Object, Object> event) {
|
||||
log.info("Removed " + messageLog(event) + " from the cache");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterUpdate(EntryEvent<Object, Object> event) {
|
||||
log.info("Updated " + messageLog(event) + " in the cache");
|
||||
}
|
||||
|
||||
private String messageLog(EntryEvent<Object, Object> event) {
|
||||
Object key = event.getKey();
|
||||
Object value = event.getNewValue();
|
||||
|
||||
if (event.getOperation().isUpdate()) {
|
||||
return "[" + key + "=" + event.getOldValue() + " to " + event.getNewValue() + "]";
|
||||
}
|
||||
return "[" + key + "=" + value + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class CommandProcessor {
|
||||
|
||||
private static final Log log = LogFactory.getLog(CommandProcessor.class);
|
||||
|
||||
boolean threadActive;
|
||||
private Thread thread;
|
||||
|
||||
void start() {
|
||||
if (thread == null) {
|
||||
threadActive = false;
|
||||
thread = new Thread(new Task(), "cmd-processor");
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
void stop() throws Exception {
|
||||
threadActive = true;
|
||||
thread.join(3 * 1000);
|
||||
}
|
||||
|
||||
private class Task implements Runnable {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
while (threadActive) {
|
||||
if (br.ready()) {
|
||||
String line = br.readLine();
|
||||
log.info("Read line " + line);
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
// just ignore any exceptions
|
||||
log.error("Caught exception while processing commands ", ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class GridToWorldWriter extends CacheWriterAdapter<Object, Object> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* Main bean for interacting with the cache from the console.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@Component
|
||||
public class HelloWorld {
|
||||
|
||||
private static final Log log = LogFactory.getLog(HelloWorld.class);
|
||||
|
||||
// inject the region
|
||||
@Resource
|
||||
private Region<String, String> region;
|
||||
|
||||
// re-inject the region (as a Map)
|
||||
// the name is required since by default Spring
|
||||
// injects the beans
|
||||
@Resource(name = "hw-region")
|
||||
private Map<String, String> map;
|
||||
|
||||
private CommandProcessor processor;
|
||||
|
||||
@PostConstruct
|
||||
void start() {
|
||||
log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId()
|
||||
+ " connecting to region [" + region.getName() + "]");
|
||||
processor = new CommandProcessor();
|
||||
processor.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void stop() throws Exception {
|
||||
log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId()
|
||||
+ " disconnecting from region [" + region.getName() + "]");
|
||||
processor.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Hello World startup class.
|
||||
* Bootstraps the Spring container which in turns starts GemFire and the actual application.
|
||||
* <p/>
|
||||
* Accepts as optional parameters location of one (or multiple) application contexts that will
|
||||
* be used for configuring the Spring container. See the reference documentation for more
|
||||
* {@link http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html information}.
|
||||
*
|
||||
* Note that in most (if not all) managed environments writing such a class is not needed
|
||||
* as Spring already provides the required integration.
|
||||
*
|
||||
* @see org.springframework.web.context.ContextLoaderListener
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
private static final String[] CONFIGS = new String[] { "app-context.xml" };
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String[] res = (args != null && args.length > 0 ? args : CONFIGS);
|
||||
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(res);
|
||||
// shutdown the context along with the VM
|
||||
ctx.registerShutdownHook();
|
||||
}
|
||||
}
|
||||
13
samples/hello-world/src/main/resources/app-context.xml
Normal file
13
samples/hello-world/src/main/resources/app-context.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<import resource="cache-context.xml"/>
|
||||
|
||||
<!-- find beans by scanning the classpath -->
|
||||
<context:component-scan base-package="org.springframework.data.gemfire.samples.helloworld"/>
|
||||
</beans>
|
||||
21
samples/hello-world/src/main/resources/cache-context.xml
Normal file
21
samples/hello-world/src/main/resources/cache-context.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- GemFire cache bean -->
|
||||
<bean id="cache" class="org.springframework.data.gemfire.CacheFactoryBean"/>
|
||||
|
||||
<!-- hello world region -->
|
||||
<!-- since no name is given, the region will be named after the bean -->
|
||||
<bean id="hw-region" class="org.springframework.data.gemfire.RegionFactoryBean" p:cache-ref="cache">
|
||||
<property name="cacheListeners">
|
||||
<bean class="org.springframework.data.gemfire.samples.helloworld.CacheLogger"/>
|
||||
</property>
|
||||
<property name="cacheWriter">
|
||||
<bean class="org.springframework.data.gemfire.samples.helloworld.GridToWorldWriter"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class BasicTest {
|
||||
|
||||
@Test
|
||||
public void testBasic() throws Exception {
|
||||
Main.main(new String[] {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user