initial commit of JMX sample

This commit is contained in:
Mark Fisher
2010-04-19 22:56:12 +00:00
parent 5d9ddcd916
commit acb90da0ee
6 changed files with 199 additions and 0 deletions

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:si="http://www.springframework.org/schema/integration"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx-2.0.xsd
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
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd">
<context:mbean-export/>
<context:mbean-server/>
<context:component-scan base-package="org.springframework.integration.samples.jmx"/>
<jmx:attribute-polling-channel-adapter channel="seconds"
object-name="org.springframework.integration.samples.jmx:type=StopWatch,name=stopWatch"
attribute-name="Seconds">
<si:poller max-messages-per-poll="1">
<si:interval-trigger interval="1" time-unit="SECONDS"/>
</si:poller>
</jmx:attribute-polling-channel-adapter>
<si:publish-subscribe-channel id="seconds"/>
<stream:stdout-channel-adapter channel="seconds" append-newline="true"/>
<si:filter input-channel="seconds" expression="payload >= 10" output-channel="reset"/>
<jmx:operation-invoking-channel-adapter id="reset"
default-object-name="org.springframework.integration.samples.jmx:type=StopWatch,name=stopWatch"
default-operation-name="reset"/>
</beans>

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2002-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.integration.samples.jmx;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Mark Fisher
* @since 2.0
*/
public class JmxAdapterDemo {
public static void main(String[] args) {
new ClassPathXmlApplicationContext(
"JmxAdapterDemo-context.xml", JmxAdapterDemo.class);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-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.integration.samples.jmx;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
/**
* @author Mark Fisher
* @since 2.0
*/
@Component
@ManagedResource
public class StopWatch implements InitializingBean {
private final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
private volatile Future<?> future;
private final AtomicInteger seconds = new AtomicInteger();
@ManagedAttribute
public int getSeconds() {
return this.seconds.get();
}
public void afterPropertiesSet() {
this.start();
}
@ManagedOperation
public void start() {
this.scheduler.initialize();
this.future = this.scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
seconds.incrementAndGet();
}
}, 1000);
}
@ManagedOperation
public void stop() {
this.future.cancel(true);
}
@ManagedOperation
public void reset() {
this.seconds.set(0);
}
}