Missing files plus @PostConstruct/@PreDestroy docs
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/25469731 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/25469731 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/25469731 Branch: refs/heads/master Commit: 25469731e697ac68e49282793761c8a08f02890f Parents: 4d27f4c Author: Jonathan Gallimore <j...@jrg.me.uk> Authored: Wed Apr 15 16:53:42 2015 +0100 Committer: Jonathan Gallimore <j...@jrg.me.uk> Committed: Wed Apr 15 17:53:06 2015 +0100 ---------------------------------------------------------------------- examples/resources-jmx-example/README.md | 111 +++++++++++++++- .../resource/jmx/resources/Alternative.java | 130 +++++++++++++++++++ .../jmx/resources/AlternativeMBean.java | 32 +++++ 3 files changed, 268 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/25469731/examples/resources-jmx-example/README.md ---------------------------------------------------------------------- diff --git a/examples/resources-jmx-example/README.md b/examples/resources-jmx-example/README.md index 81c5e7c..9a76cd2 100644 --- a/examples/resources-jmx-example/README.md +++ b/examples/resources-jmx-example/README.md @@ -241,14 +241,115 @@ Resources are typically discovered, created, and bound to JNDI very early on in The following properties can be used to change this behavior. -| Property | Values | Description | -| -------------------------- | -----| -| Lazy | true/false | Creates a proxy that defers the actual instantiation of the resource until the first time it is looked up from JNDI. | -| UseAppClassLoader | true/false | Forces a lazily instantiated resource to use the application classloader, instead of the classloader available when the resources were first processed. | -| InitializeAfterDeployment | true/false | Forces a resource created with the Lazy property to be instantiated once the application has started, as opposed to waiting for it to be looked up. | +| Property | Values | Description | +| -------------------------- | ---------------- | ----------- | +| Lazy | true/false | Creates a proxy that defers the actual instantiation of the resource until the first time it is looked up from JNDI. | +| UseAppClassLoader | true/false | Forces a lazily instantiated resource to use the application classloader, instead of the classloader available when the resources were first processed. | +| InitializeAfterDeployment | true/false | Forces a resource created with the Lazy property to be instantiated once the application has started, as opposed to waiting for it to be looked up. | By default, if TomEE encounters a custom application resource that cannot be instantiated until the application has started, it will set these three flags to `true`, unless the `Lazy` flag has been explicitly set. +# PostConstruct / PreDestroy + +As an alternative to using a factory method, you can use @PostConstruct and @PreDestroy methods within your resource class (note that you cannot use this within a factory class) to manage any additional creation or cleanup activities. TomEE will automatically call these methods when the application is started and destroyed. Using @PostConstruct will effectively force a lazily loaded resource to be instantiated when the application is starting - in the same way that the `InitializeAfterDeployment` property does. + + public class Alternative implements AlternativeMBean { + + private static Logger LOGGER = Logger.getLogger(Alternative.class.getName()); + private Properties properties; + + @PostConstruct + public void postConstruct() throws MBeanRegistrationException { + // initialize the bean + + final String code = properties.getProperty("code"); + final String name = properties.getProperty("name"); + + requireNotNull(code); + requireNotNull(name); + + try { + final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + final ObjectName objectName = new ObjectName(name); + mbs.registerMBean(this, objectName); + } catch (final MalformedObjectNameException e) { + LOGGER.severe("Malformed MBean name: " + name); + throw new MBeanRegistrationException(e); + } catch (final InstanceAlreadyExistsException e) { + LOGGER.severe("Instance already exists: " + name); + throw new MBeanRegistrationException(e); + } catch (final NotCompliantMBeanException e) { + LOGGER.severe("Class is not a valid MBean: " + code); + throw new MBeanRegistrationException(e); + } catch (final javax.management.MBeanRegistrationException e) { + LOGGER.severe("Error registering " + name + ", " + code); + throw new MBeanRegistrationException(e); + } + } + + @PreDestroy + public void preDestroy() throws MBeanRegistrationException { + final String name = properties.getProperty("name"); + requireNotNull(name); + + try { + final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + final ObjectName objectName = new ObjectName(name); + mbs.unregisterMBean(objectName); + } catch (final MalformedObjectNameException e) { + LOGGER.severe("Malformed MBean name: " + name); + throw new MBeanRegistrationException(e); + } catch (final javax.management.MBeanRegistrationException e) { + LOGGER.severe("Error unregistering " + name); + throw new MBeanRegistrationException(e); + } catch (InstanceNotFoundException e) { + LOGGER.severe("Error unregistering " + name); + throw new MBeanRegistrationException(e); + } + } + + private void requireNotNull(final String object) throws MBeanRegistrationException { + if (object == null) { + throw new MBeanRegistrationException("code property not specified, stopping"); + } + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(final Properties properties) { + this.properties = properties; + } + + private int count = 0; + + @Override + public String greet(String name) { + if (name == null) { + throw new NullPointerException("Name cannot be null"); + } + + return "Hello, " + name; + } + + @Override + public int getCount() { + return count; + } + + @Override + public void setCount(int value) { + count = value; + } + + @Override + public void increment() { + count++; + } + } + + # Running Running the example can be done from maven with a simple 'mvn clean install' command run from the 'resources-jmx-example' directory. http://git-wip-us.apache.org/repos/asf/tomee/blob/25469731/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/Alternative.java ---------------------------------------------------------------------- diff --git a/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/Alternative.java b/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/Alternative.java new file mode 100644 index 0000000..546c53a --- /dev/null +++ b/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/Alternative.java @@ -0,0 +1,130 @@ +/* + * 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.superbiz.resource.jmx.resources; + +import org.superbiz.resource.jmx.factory.MBeanRegistrationException; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; +import java.lang.management.ManagementFactory; +import java.util.Properties; +import java.util.logging.Logger; + +public class Alternative implements AlternativeMBean { + + private static Logger LOGGER = Logger.getLogger(Alternative.class.getName()); + private Properties properties; + + @PostConstruct + public void postConstruct() throws MBeanRegistrationException { + // initialize the bean + + final String code = properties.getProperty("code"); + final String name = properties.getProperty("name"); + + requireNotNull(code); + requireNotNull(name); + + try { + final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + final ObjectName objectName = new ObjectName(name); + mbs.registerMBean(this, objectName); + } catch (final MalformedObjectNameException e) { + LOGGER.severe("Malformed MBean name: " + name); + throw new MBeanRegistrationException(e); + } catch (final InstanceAlreadyExistsException e) { + LOGGER.severe("Instance already exists: " + name); + throw new MBeanRegistrationException(e); + } catch (final NotCompliantMBeanException e) { + LOGGER.severe("Class is not a valid MBean: " + code); + throw new MBeanRegistrationException(e); + } catch (final javax.management.MBeanRegistrationException e) { + LOGGER.severe("Error registering " + name + ", " + code); + throw new MBeanRegistrationException(e); + } + } + + @PreDestroy + public void preDestroy() throws MBeanRegistrationException { + final String name = properties.getProperty("name"); + requireNotNull(name); + + try { + final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + final ObjectName objectName = new ObjectName(name); + mbs.unregisterMBean(objectName); + } catch (final MalformedObjectNameException e) { + LOGGER.severe("Malformed MBean name: " + name); + throw new MBeanRegistrationException(e); + } catch (final javax.management.MBeanRegistrationException e) { + LOGGER.severe("Error unregistering " + name); + throw new MBeanRegistrationException(e); + } catch (InstanceNotFoundException e) { + LOGGER.severe("Error unregistering " + name); + throw new MBeanRegistrationException(e); + } + } + + private void requireNotNull(final String object) throws MBeanRegistrationException { + if (object == null) { + throw new MBeanRegistrationException("code property not specified, stopping"); + } + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(final Properties properties) { + this.properties = properties; + } + + private int count = 0; + + @Override + public String greet(String name) { + if (name == null) { + throw new NullPointerException("Name cannot be null"); + } + + return "Hello, " + name; + } + + @Override + public int getCount() { + return count; + } + + @Override + public void setCount(int value) { + count = value; + } + + @Override + public void increment() { + count++; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/25469731/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/AlternativeMBean.java ---------------------------------------------------------------------- diff --git a/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/AlternativeMBean.java b/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/AlternativeMBean.java new file mode 100644 index 0000000..65e9076 --- /dev/null +++ b/examples/resources-jmx-example/resources-jmx-ejb/src/main/java/org/superbiz/resource/jmx/resources/AlternativeMBean.java @@ -0,0 +1,32 @@ +/* + * 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.superbiz.resource.jmx.resources; + +public interface AlternativeMBean { + + public String greet(final String name); + + public int getCount(); + + public void setCount(int count); + + public void increment(); + +}