Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The...

21
Spring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed resources (MBeans) in a JMX environment. Exposing Spring Beans with Spring JMX allows them to be accessed and managed as standard MBeans. In the context of J2EE containers that make extensive use of JMX, this enables Spring Beans to be treated as standard resources within the container. For J2EE containers such as Oracle Containers for J2EE (OC4J) that provide direct access to JMX MBeans from the management console, the Spring MBeans can be viewed and managed from the same administration console used to manage the server. This paper provides a simple example of a JavaBean that is exposed as a Spring Bean. The Spring Bean is then packaged and included within a J2EE Web application and accessed from a JSP page. The Spring configuration file is then configured so that the Spring Bean is exported as an MBean and wired into an MBeanServer running within OC4J. Using the Application Server Control management console supplied with OC4J, the Spring MBean is shown as an MBean from which its properties can be updated, and the results are immediately seen in the viewing JSP page. A Basic Spring MBean The basic Spring Bean used as an example here is loosely based on the GreetingService example from the Spring in Action book from Manning Press. The Java Bean used in this example as a Spring Bean is comprised of an interface: package sab.test.spring.beans; public interface GreetingService { public String sayGreeting(); public String discoverLanguage(); } And its implementation class: package sab.test.spring.beans; public class GreetingServiceImpl implements GreetingService { private String greeting; private String language;

Transcript of Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The...

Page 1: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Spring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed resources (MBeans) in a JMX environment. Exposing Spring Beans with Spring JMX allows them to be accessed and managed as standard MBeans. In the context of J2EE containers that make extensive use of JMX, this enables Spring Beans to be treated as standard resources within the container. For J2EE containers such as Oracle Containers for J2EE (OC4J) that provide direct access to JMX MBeans from the management console, the Spring MBeans can be viewed and managed from the same administration console used to manage the server. This paper provides a simple example of a JavaBean that is exposed as a Spring Bean. The Spring Bean is then packaged and included within a J2EE Web application and accessed from a JSP page. The Spring configuration file is then configured so that the Spring Bean is exported as an MBean and wired into an MBeanServer running within OC4J. Using the Application Server Control management console supplied with OC4J, the Spring MBean is shown as an MBean from which its properties can be updated, and the results are immediately seen in the viewing JSP page.

A Basic Spring MBean The basic Spring Bean used as an example here is loosely based on the GreetingService example from the Spring in Action book from Manning Press. The Java Bean used in this example as a Spring Bean is comprised of an interface: package sab.test.spring.beans; public interface GreetingService { public String sayGreeting(); public String discoverLanguage(); } And its implementation class: package sab.test.spring.beans; public class GreetingServiceImpl implements GreetingService { private String greeting; private String language;

Page 2: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

public GreetingServiceImpl() {} public GreetingServiceImpl(String greeting) { this.greeting = greeting; } public void setGreeting(String greeting) { this.greeting = greeting; } public String getGreeting() { return this.greeting; } public void setLanguage(String language) { this.language = language; } public String getLanguage() { return this.language; } public String sayGreeting() { return greeting; } public String discoverLanguage() { return language; } } To use this Java Bean with the Spring Framework, a Spring configuration file must first be created. The Spring Framework enables properties on the bean to be injected from definitions in the Spring configuration file, so that when the bean is accessed the requisite properties are set. This example of the configuration file creates two Spring Beans instances using our Java Bean class – italianGreeting and aussieGreeting. The Spring Framework injects the language and greeting properties as defined in the configuration file. <?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="italianGreetingService" class="sab.test.spring.beans.GreetingServiceImpl"> <property name="greeting"> <value>ciao!</value> </property> <property name="language"> <value>Italian</value> </property> </bean> <bean id="aussieGreetingService" class="sab.test.spring.beans.GreetingServiceImpl"> <property name="greeting">

Page 3: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

<value>gday mate!</value> </property> <property name="language"> <value>Aussie</value> </property> </bean> </beans> As we’ll see, a J2EE Web application can be configured to easily load the Spring Framework and access these Spring Beans. First off, configure the Web application to load the Spring configuration file. This is done in the web.xml of the Web application. <?xml version = '1.0' encoding = 'windows-1252'?> <web-app> <description>simple Spring</description> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/greetingservice.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> Accessing the Spring Beans from a JSP page is done with a simple namebased lookup. The returned beans can then be used as defined via their interface. <h3>The GreetingService Beans</h3> <% ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application); GreetingService aussieGreeting = (GreetingService) ctx.getBean("aussieGreetingService"); GreetingService italianGreeting = (GreetingService) ctx.getBean("italianGreetingService"); %> <p> An <%=aussieGreeting.discoverLanguage()%> says “<%=aussieGreeting.sayGreeting()%>" </p> <p> An <%=italianGreeting.discoverLanguage()%> says "<%=italianGreeting.sayGreeting()%>" </p>

Page 4: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Packaging this into a WAR file and deploying to OC4J enables the JSP to be accessed from a browser client.

The Spring configuration file in the deployed application directory can be edited to modify the properties of the Spring Bean.. <bean id="italianGreetingService" class="sab.test.spring.beans.GreetingServiceImpl"> <constructor-arg> <value>ciao bella!</value> </constructor-arg> <property name="language"> <value>Italian</value> </property> </bean> Finally, the application is restarted to pick up the change in the configuration file.

Page 5: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

The JSP page shows the results of the change.

Page 6: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Exporting Spring Beans as JMX MBeans Using the built-in support Spring has for JMX, exposing the Spring Beans in the configuration file as JMX MBeans is quite simple and can be done by adding a few extra lines to the configuration file. The first thing needed is to define a reference to an MBeanServer using the Spring MBeanServerFactoryBean. When deploying to OC4J a custom MBeanServer instance needs to be used because of the security proxy model that is employed by default to isolate the OC4J System MBeans from applications. <bean id="greetingMbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"> <property name="defaultDomain"> <value>GreetingService</value> </property> </bean> Once the MBeanServer Spring Bean has been configured, the list of Spring Beans to expose as MBeans can be defined using yet another member of the Spring Bean family – the MbeanExporter. For each Spring Bean to export, a JMX MBean Object Name is specified as the key, which serves as the name under which the Spring Bean will be exposed in the JMX environment. First, the MBeanServer to create the MBeans in is specified as a server property: <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="bean:name=aussieGreetingService" value-ref="aussieGreetingService" /> <entry key="bean:name=italianGreetingService" value-ref="italianGreetingService" />

Page 7: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

</map> </property> <property name="server" ref="greetingMbeanServer" /> </bean> With this new configuration in place and the application redeployed to OC4J, the JMX MBeans can be accessed directly from the Web-based Application Server Control management console as “Application Defined MBeans”.

Clicking the “Application Defined MBeans” link for the Spring application will launch the Application MBean Server. The Spring Beans that have been exported as MBeans are listed under the beans node in the tree.

Page 8: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Selecting an MBean loads the dynamic MBean instance that was created from the Spring Bean interface into the browser. Through the browser you can view the MBean’s attributes, which are analogous to the public properties on the bean; and operations, which are analogous to public operations on the bean. Modifiable property values can be edited directly in the browser. Note that changing a property on a Spring Bean using its MBean results in the change being applied immediately to the actual bean object.

Page 9: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

In our simple example, changing the properties of the aussieGreetingService is done using the property fields in the MBean browser.

Page 10: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

The JSP page that uses the aussieGreetingService then reflects the changes that were made to the properties.

Page 11: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

The properties of the italianGreetingService can be changed in a similar manner.

Page 12: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Note that the property changes are seen immediately in the JSP.

Page 13: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

In summary, we have seen how using the JMX facilities from within the Spring Framework enables Spring Bean definitions to be exposed as MBeans. When deployed to OC4J, the Spring Beans’ MBeans are created within the OC4J JMX environment and can be viewed and managed from the Application Server Control management console. So there you have it – Spring Beans being managed from Application Server Control.

Appendix A: Deploying a WAR Module to OC4J The art of deployment. The deployment of an application to OC4J can be performed using its Application Server Control management console. The management console is accessed from your OC4J instance with the “/em” URL. For example: http://localhost:8888/em Login with the username “oc4jadmin” and the password that was set for this user when OC4J was first started (or installed in the case of Oracle Application Server).

Page 14: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

This will open the home page of ASC. Select the “Applications” tab to view the list of applications deployed to this OC4J instance.

Click the “Deploy” button to invoke the deployment wizard.

Page 15: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Specify the path to the application to be deployed. The application can be an archive of EAR, JAR, WAR or RAR form. Use the “Browse” button to locate the archive on the local file system.

Page 16: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

After specifying the archive, click the “Next” button to upload the archive to the server.

Page 17: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Enter the name the application will use when it is deployed to the server. For each Web module being deployed, specify the context-root to which it will be bound and made available to clients. Click “Next” to proceed.

Page 18: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

A summary is presented from the deployment information collected so far. Advanced deployment configuration tasks can be performed on this page, including configuring security and EJB mappings, classloading and clustering configurations. Unless these are required, click the “Deploy” button to initiate the actual deployment operation.

Page 19: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

As the deployment operation proceeds, progress messages are displayed. If the deployment operation fails, a sequence of errors messages will be displayed which will help indicate what problem was encountered. If the deployment operation succeeds, a summary of the operation and a success message is shown. Click the “Return” button to return to the applications page.

Page 20: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

If the deployment operation succeeds, the newly deployed application will be listed as a deployed application using the name specified.

Page 21: Spring Soup with OC4J and MBeans - OracleSpring Soup with OC4J and MBeans Steve Button 4/27/2007 The Spring Framework includes support for dynamically exposing Spring Beans as managed

Appendix B: Packaging Spring in a Web Application The art of packaging and wrapping with pretty bows. [tbd]