= helloworld-mbean: Helloworld Using MBean and CDI component
:author: Lagarde Jeremie
:productName: WildFly
:productNameFull: WildFly Application Server
:jbossHomeName: WILDFLY_HOME
:productVersion: 12
:buildRequirements: Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later
include::../shared-doc/attributes.adoc[]

:level: Intermediate
:technologies: CDI, JMX, MBean
:source: {githubRepoUrl}
:imagesdir: images/

[abstract]
The `helloworld-mbean` quickstart demonstrates the use of CDI and MBean in {productName} and includes JConsole instructions and Arquillian tests.

:standalone-server-type: default
:archiveType: war

== What is it?

The `helloworld-mbean` quickstart demonstrates the use of _CDI_ and _MBean_ in {productNameFull}. The project also includes a set of Arquillian tests for MBeans.

The example is composed of the following MBeans.

[cols="30%,70%",options="headers"]
|===
|MBean |Description

| `AnnotatedComponentHelloWorld`
| This MBean is a managed bean with `@MXBean` annotation.

| `MXComponentHelloWorld`
| This MBean is a managed bean with `MXBean` interface.

| `MXPojoHelloWorld`
| This MBean is a POJO using `MXBean` interface and declared in the `jboss-service.xml` file.

| `SarMXPojoHelloWorld`
| This MBean is a pojo using `MXBean` interface and declared in the `jboss-service.xml` file using SAR packaging.
|===

//*************************************************
// Add System Requirements
//*************************************************
// == System Requirements
include::../shared-doc/system-requirements.adoc[leveloffset=+1]

//*************************************************
// Add Use of JBoss Home Name
//*************************************************
// == Use of {jbossHomeName}
include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]

//*************************************************
// Start the server with the default profile
//*************************************************
// == Start the {productName} Standalone Server
include::../shared-doc/start-the-standalone-server.adoc[leveloffset=+1]

//*************************************************
// Build and deploy the quickstart JAR
//*************************************************
// == Build and Deploy the Quickstart
:archiveName: {artifactId}-service
include::../shared-doc/build-and-deploy-the-quickstart.adoc[leveloffset=+1]

// Additional deployment information
This also deploys the `{artifactId}-service/target/{artifactId}-service.sar` to the running instance of the server.

== Access and Test the MBeans

This quickstart differs from the other quickstarts in that it uses _JConsole_ to access and test the quickstart rather than access an URL in the browser. If you do access http://localhost:8080/{artifactId}-webapp/, you will see a screen shot image of the _JConsole_ application,

The following sections describe how to use _JConsole_ to inspect and test the MBeans.

[[start_jconsole]]
== Start JConsole

. To connect to the {productName} server using _JConsole_, open a terminal and type the following command, replacing __JDK_HOME__ with the path to your JDK installation :
+
[source,subs="+quotes,attributes+",options="nowrap"]
----
__JDK_HOME__/bin/jconsole
----
NOTE: For Windows, type `JDK_HOME\bin\jconsole.exe`.

. Select the local *org.jboss.modules.Main* process and click *Connect*.
+
image:jconsole_connection.png[MBeans in JConsole Connection]

. A dialog displays with the warning: `Secure connection failed. Retry insecurely?`

. Click *Insecure* to continue.

[[test_the_mbeans_in_jconsole]]
== Test the MBeans in JConsole

You can use _JConsole_ to inspect and use the MBeans :
image:jconsole.png[MBeans in JConsole]

. Click on the MBeans tab.
. Expand *quickstarts* in the left column of the console.
. Under *quickstarts*, you see the 4 MBeans: *AnnotatedComponentHelloWorld*, *MXComponentHelloWorld*, *MXPojoHelloWorld*, and *SarMXPojoHelloWorld*
. Expand each MBean and choose: *Operations* -> *sayHello*.
. Type your name in the *(p0 String )* input text box and click the *sayHello* button.
* For the *AnnotatedComponentHelloWorld* and *MXComponentHelloWorld* examples, you will see a popup Window displaying *Hello <your name>!*.
* For the *MXPojoHelloWorld* and *SarMXPojoHelloWorld* examples, you will see a popup Window displaying *Welcome <your name>!*.

//*************************************************
// Undeploy the quickstart archive
//*************************************************
// == Undeploy the Quickstart
include::../shared-doc/undeploy-the-quickstart.adoc[leveloffset=+1]


//*************************************************
// Run the Arquillian tests
//*************************************************
// == Run the Arquillian Tests
include::../shared-doc/run-arquillian-tests.adoc[leveloffset=+1]


//*************************************************
// Add JBoss Developer Studio instructions
//*************************************************
// == Run the Quickstart in Red Hat JBoss Developer Studio or Eclipse
include::../shared-doc/run-the-quickstart-in-jboss-developer-studio.adoc[leveloffset=+1]

// Additional JBoss Developer Studio instructions
This quickstart consists of multiple projects and requires installation of the `JBoss Tools Maven Packaging Configurator`, so it deploys and runs differently in JBoss Developer Studio than the other quickstarts.

. Install the JBoss Tools Maven Packaging Configurator
** If the *Red Hat Central* page is not showing, open it by choosing *Help* -> *Red Hat Central*.
** Click the *Software/Update* tab at the bottom of the *Red Hat Central*.
** Scroll down to the *Maven* section, select the *JBoss Tools Maven Packaging Configurator* and click *Install/Update*.
. Right click on the parent *{artifactId}* parent project and choose *Maven* -> *Update Project...*. Select all projects and click *OK*.
. Right-click on the *{artifactId}-service* project and choose *Run As* -> *Run on Server*.
. Right-click on the *{artifactId}-webapp* project and choose *Run As* -> *Run on Server*.
. xref:start_jconsole[Start JConsole] and xref:test_the_mbeans_in_jconsole[Test the MBeans in JConsole] as described above.
. To undeploy the web application, right-click on the *{artifactId}-wepapp* project and choose *Run As* -> *Maven build*. Enter `wildfly:undeploy` for the *Goals* and click *Run*.
. To undeploy the web service, right-click on the *{artifactId}-service* project and choose *Run As* -> *Maven build*. Enter `wildfly:undeploy` for the *Goals* and click *Run*.

== Debug the Application

If you want to debug the source code of any library in the project, run the following command to pull the source into your local repository. The IDE should then detect it.

----
$ mvn dependency:sources
----
