You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2011/06/23 17:32:53 UTC
svn commit: r1138936 - in /openejb/trunk/openejb3/examples: ./
mbean-auto-registration/ mbean-auto-registration/src/
mbean-auto-registration/src/main/ mbean-auto-registration/src/main/java/
mbean-auto-registration/src/main/java/org/ mbean-auto-registra...
Author: rmannibucau
Date: Thu Jun 23 15:32:52 2011
New Revision: 1138936
URL: http://svn.apache.org/viewvc?rev=1138936&view=rev
Log:
adding mbean-auto-registration example
Added:
openejb/trunk/openejb3/examples/mbean-auto-registration/
openejb/trunk/openejb3/examples/mbean-auto-registration/README.md (with props)
openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml (with props)
openejb/trunk/openejb3/examples/mbean-auto-registration/src/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java (with props)
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml (with props)
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/
openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java (with props)
Modified:
openejb/trunk/openejb3/examples/pom.xml
Added: openejb/trunk/openejb3/examples/mbean-auto-registration/README.md
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/mbean-auto-registration/README.md?rev=1138936&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/mbean-auto-registration/README.md (added)
+++ openejb/trunk/openejb3/examples/mbean-auto-registration/README.md Thu Jun 23 15:32:52 2011
@@ -0,0 +1,71 @@
+# MBean auto registration
+
+[Download as zip](dynamic-datasource-routing.zip)
+
+This example shows how to automatically create and register mbeans using OpenEJB features.
+
+# The MBean
+
+The mbean implements a simple game where the goal is to guess a number.
+
+It allows the user to change the value too.
+
+# The registering
+
+To register a MBean using OpenEJB you simply have to specify a property eaiher in system.properties,
+or in intial context properties.
+
+The example MBean is called org.superbiz.mbean.GuessHowManyMBean so we simply add it to the properties
+given to the initial context:
+
+ Properties properties = new Properties();
+ properties.setProperty("openejb.user.mbeans.list", GuessHowManyMBean.class.getName());
+ EJBContainer.createEJBContainer(properties);
+
+# The implementation
+
+To implement the [org.superbiz.mbean.GuessHowManyMBean](src/main/java/org/superbiz/mbean/GuessHowManyMBean.java.html)
+MBean you simply annotate the POJO with specific annotations:
+
+ @MBean
+ @Description("play with me to guess a number")
+ public class GuessHowManyMBean {
+ private int value = 0;
+
+ @ManagedAttribute
+ @Description("you are cheating!")
+ public int getValue() {
+ return value;
+ }
+
+ @ManagedAttribute
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ @ManagedOperation
+ public String tryValue(int userValue) {
+ if (userValue == value) {
+ return "winner";
+ }
+ return "not the correct value, please have another try";
+ }
+ }
+
+# Tests
+
+Then simply get the platform server and you can play with parameters and operations:
+
+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+ ObjectName objectName = new ObjectName(OBJECT_NAME);
+ MBeanInfo infos = server.getMBeanInfo(objectName);
+ assertEquals(0, server.getAttribute(objectName, "value"));
+ server.setAttribute(objectName, new Attribute("value", 3));
+ assertEquals(3, server.getAttribute(objectName, "value"));
+ assertEquals("winner", server.invoke(objectName, "tryValue", new Object[]{3}, null));
+ assertEquals("not the correct value, please have another try", server.invoke(objectName, "tryValue", new Object[]{2}, null));
+
+# Note
+
+If OpenEJB can't find any module it can't start. So to force him to start even if the example has only the mbean
+as java class, we added a beans.xml file to let OpenEJB find a module.
Propchange: openejb/trunk/openejb3/examples/mbean-auto-registration/README.md
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml?rev=1138936&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml (added)
+++ openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml Thu Jun 23 15:32:52 2011
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.superbiz</groupId>
+ <artifactId>mbean-auto-registration</artifactId>
+ <packaging>jar</packaging>
+ <version>1.1-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: MBean Auto Registration</name>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <openejb.version>4.0.0-SNAPSHOT</openejb.version>
+ </properties>
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>apache-m2-snapshot</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://repository.apache.org/snapshots</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>javaee-api</artifactId>
+ <version>6.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <!--
+ The router is dependent of openejb so it needs openejb core with compile scope.
+ -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>openejb-core</artifactId>
+ <version>${openejb.version}</version>
+ </dependency>
+ </dependencies>
+ <!--
+ This section allows you to configure where to publish libraries for sharing.
+ It is not required and may be deleted. For more information see:
+ http://maven.apache.org/plugins/maven-deploy-plugin/
+ -->
+ <distributionManagement>
+ <repository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/repo/</url>
+ </repository>
+ <snapshotRepository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/snapshot-repo/</url>
+ </snapshotRepository>
+ </distributionManagement>
+</project>
Propchange: openejb/trunk/openejb3/examples/mbean-auto-registration/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java?rev=1138936&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java (added)
+++ openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java Thu Jun 23 15:32:52 2011
@@ -0,0 +1,34 @@
+package org.superbiz.mbean;
+
+import javax.management.Description;
+import javax.management.MBean;
+import javax.management.ManagedAttribute;
+import javax.management.ManagedOperation;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+@MBean
+@Description("play with me to guess a number")
+public class GuessHowManyMBean {
+ private int value = 0;
+
+ @ManagedAttribute
+ @Description("you are cheating!")
+ public int getValue() {
+ return value;
+ }
+
+ @ManagedAttribute
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ @ManagedOperation
+ public String tryValue(int userValue) {
+ if (userValue == value) {
+ return "winner";
+ }
+ return "not the correct value, please have another try";
+ }
+}
Propchange: openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/java/org/superbiz/mbean/GuessHowManyMBean.java
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml?rev=1138936&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml (added)
+++ openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml Thu Jun 23 15:32:52 2011
@@ -0,0 +1 @@
+<beans />
Propchange: openejb/trunk/openejb3/examples/mbean-auto-registration/src/main/resources/META-INF/beans.xml
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java?rev=1138936&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java (added)
+++ openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java Thu Jun 23 15:32:52 2011
@@ -0,0 +1,36 @@
+package org.superbiz.mbean;
+
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.management.Attribute;
+import javax.management.MBeanInfo;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotSame;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+public class GuessHowManyMBeanTest {
+ private static final String OBJECT_NAME = "openejb.user.mbeans:group=org.superbiz.mbean,application=mbean-auto-registration,name=GuessHowManyMBean";
+
+ @Test public void play() throws Exception {
+ Properties properties = new Properties();
+ properties.setProperty("openejb.user.mbeans.list", GuessHowManyMBean.class.getName());
+ EJBContainer.createEJBContainer(properties);
+
+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+ ObjectName objectName = new ObjectName(OBJECT_NAME);
+ MBeanInfo infos = server.getMBeanInfo(objectName);
+ assertEquals(0, server.getAttribute(objectName, "value"));
+ server.setAttribute(objectName, new Attribute("value", 3));
+ assertEquals(3, server.getAttribute(objectName, "value"));
+ assertEquals("winner", server.invoke(objectName, "tryValue", new Object[]{3}, null));
+ assertEquals("not the correct value, please have another try", server.invoke(objectName, "tryValue", new Object[]{2}, null));
+ }
+}
Propchange: openejb/trunk/openejb3/examples/mbean-auto-registration/src/test/java/org/superbiz/mbean/GuessHowManyMBeanTest.java
------------------------------------------------------------------------------
svn:executable = *
Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=1138936&r1=1138935&r2=1138936&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Thu Jun 23 15:32:52 2011
@@ -78,6 +78,7 @@
<module>testing-security-meta</module>
<module>decorators</module>
<module>dynamic-datasource-routing</module>
+ <module>mbean-auto-registration</module>
</modules>
<profiles>
<profile>