Download - JBoss AS 7 - development basics
JBOSS AS 7 DEVELOPMENT BASICS WITH JAVA EE
Gabriele Falace
INSTALLATION AND INITIAL
SETUP
JBoss AS 7 | Gabriele Falace
Introduction
• JBoss Application Server 7 is a Java EE compliant
application server
• Provides the runtime to any portable Java EE application.
• Other than basic containers (web and EJB) it offers a
whole set of components supporting extra features such
as REST services, security and more.
JBoss AS 7 | Gabriele Falace
Components overview
• Here are some components shipped with JBoss AS 7
• Hibernate 4 (for Object Relational Mapping)
• RESTEasy (for developing REST services)
• Mojarra (a standard implementation of JSF)
• Infinispan (a container managed Cache service)
• Apache CXF (for developing SOAP services)
• Arquillian (testing framework)
JBoss AS 7 | Gabriele Falace
Initial setup To start using JBoss
• Set the JBOSS_HOME environment variable to the JBoss installation’s root directory (where it has been decompressed).
• Make sure that JAVA_HOME and MAVEN_HOME variables are correctly pointing Java and Maven installation directories, respectively
• Add a user with jboss-7.x.x.x\bin\add-user.bat
• Choose a)
• Realm let ManagementRealm
• User type your own username
• Password type your own password
• Start the server with the script jboss-7.x.x.x\bin\standalone.bat
• Pointing the browser to http://localhost:8080 we can test that the server has started correctly and we can then access the console (also reachable at http://localhost:9990 )
JBoss AS 7 | Gabriele Falace
Initial setup
JBoss AS 7 | Gabriele Falace
Initial setup
• In addition to the console, administration tasks can be
accomplished via a command line interface (CLI) started
with the script
jboss-7.x.x.x\bin\jboss-admin.bat –connect
• JBoss tools for Eclipse:
http://marketplace.eclipse.org/content/jboss-tools-kepler
will enhance Eclipse with several add-ons which will
streamline Enterprise Application development with
JBoss.
JBoss AS 7 | Gabriele Falace
DATASOURCES
JBoss AS 7 | Gabriele Falace
Configuring a Datasource
• The ‘safest’ way to create a Datasource is creating a so-
called Module, through the following procedure (i.e.
MySQL)
1. Browse to the following directory: jboss-as-7.1.0.Final\modules\com
and create the following directory: mysql\main
obtaining jboss-as-7.1.0.Final\modules\com\mysql\main
2. In such location, copy the MySQL java driver’s Jar, that
should be named something similare to: mysql-connector-java-5.1.27-bin.jar
JBoss AS 7 | Gabriele Falace
Configuring a datasource
3. In the same directory create the file module.xml with the following content: <module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.27-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
4. In the directory: jboss-as-7.1.0.Final\standalone\configuration open the standalone.xml file
Note: it is also easily accessible in Eclipse in Filesets > Configuration File under the ‘Servers’ tab.
JBoss AS 7 | Gabriele Falace
Configuring a Datasource
5. In the standalone.xml insert • Inside the <drivers> tag, add the following
<driver name="mysql" module="com.mysql"/>
• Inside the <datasources> tag, add the following <datasource jta="true" jndi-name="java:/mydb" pool-name="my_pool" enabled="true" use-java-context="true" use-ccm="true">
<connection-url> jdbc:mysql://localhost:3306/world
</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
<statement>
<prepared-statement-cache-size>100</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
JBoss AS 7 | Gabriele Falace
Using a DataSource
6. The DataSource is now ready to be used. In our
persistence.xml file, we can point the datasource, as
shown in the following element:
<jta-data-source>java:/mydb</jta-data-source>
JBoss AS 7 | Gabriele Falace
HORNETQ AND JMS
JBoss AS 7 | Gabriele Falace
JMS Queues and HornetQ
In order to utilize the HornetQ messaging middleware, one
needs to use the standalone-full.xml configuration file
To start the ‘full’ server in Eclipse
1. Double click on the server
2. Under ‘General Information’ click on ‘Runtime Environment’
3. In the ‘Configuration file’ box, choose standalone-full.xml
The config file already contains proper settings to use the
system. In the following, let’s see what is specified in that
file
JBoss AS 7 | Gabriele Falace
JMS Queues and HornetQ
• To declare a queue, so that it is deployed on the server, some
configuration inside the config file has to be done.
• Inside the <hornetq-server> element, insert the following
<jms-destinations>
<jms-queue name="testQueue">
<entry name="queue/test"/>
<entry name="java:jboss/exported/jms/queue/test"/>
</jms-queue>
<jms-topic name="testTopic">
<entry name="topic/test"/>
<entry name="java:jboss/exported/jms/topic/test"/>
</jms-topic>
</jms-destinations>
JBoss AS 7 | Gabriele Falace
An MDB processing JMS messages
JBoss AS 7 | Gabriele Falace
A JMS sender
JBoss AS 7 | Gabriele Falace
INFINISPAN
JBoss AS 7 | Gabriele Falace
INFINISPAN
• It’s JBoss’ cache, also used by several other modules
• Infinispan libraries, located in JBOSS_HOME/modules/org/infinispan
must be visible to the project (i.e. add to Build path)
• In order to get them exported when a deployable artifact is created,
the following line must be appended to the Manifest.MF file
Dependencies: org.infinispan export
• Now we can use JBoss’ Infinispan cache subsystem, by injecting the
CacheContainer through the @Resource annotation
JBoss AS 7 | Gabriele Falace
INFINISPAN
JBoss AS 7 | Gabriele Falace
CLUSTERING
JBoss AS 7 | Gabriele Falace
Clustering - intro
High availability
• Obtained through components replication (EJBs, HTTP session) across several nodes/instances
• Namely, each physical machine is a ‘Node’ on which several JBoss AS instances may run
• But replication requires keeping replicated objects coherent and therefore it can affect scalability (especially in case of stateful objects)
• When Stateful EJBs are replicated, these get activated/passivated after each invocation and the state is managed through the infinispan cache.
JBoss AS 7 | Gabriele Falace
Clustering – application side
• On the application side, a couple of operations need to be
performed
1. In the web.xml file, adding the element <distributable/>
right before the closing </web-app> tag
2. Annotate the EJBs that we want to be replicated (or clustered) with the @Clustered annotation from the
org.jboss.ejb3.annotation package
JBoss AS 7 | Gabriele Falace
Clustering – configuring
• Moreover, we need to
• Install N JBoss instances, decompressing to separate folders
• Install an Apache HTTPD server, to act as a load balancer
• Install into httpd the mod_cluster module
• On the JBoss website there’s an http server with mod_cluster
already bundled
• Pay attention to other applications that possibly could use same
ports used by JBoss, thus interfering with the server behavior
• On windows, use the netstat –noa
command to check what ports are being used by which process
JBoss AS 7 | Gabriele Falace
Clustering – configuring
• Start each instance with the standalone-ha.xml config file
• Except the first instance, it is also needed to set port
offsets, not to make instances collide on ports acquisition
standalone.bat -Djboss.node.name=jb2 --server-config=standalone-ha.xml -Djboss.socket.binding.port-offset=100
• Correct deployment can be checked by verifying the
presence of a file named projectName.ear.deployed in the
deployment directory
JBoss AS 7 | Gabriele Falace
Clustering - failover
At this point, we can start our mod_cluster enabled httpd server: JBoss instances currently running will automatically register with the httpd server, as a load balancer (a message will appear in the instances console).
When we turn one of the JBoss instances down, httpd will automatically manage the failover forwarding requests to an active instance, in a way totally transparent to the application.
The application needs to be accessed by pointing the browser to the address:port where the HTTP Server is listening.
JBoss AS 7 | Gabriele Falace
APPLICATION SECURITY
JBoss AS 7 | Gabriele Falace
Security – basic concepts
Main security concerns are
• Authentication (Principal, a.k.a. Users)
• Authorization (Roles, operations allowed on which resources)
EJB 3 allow to profile access to methods based on roles, by annotating such methods: @PermitAll any caller can execute this method
@RolesAllowed("FOO") this method is executed only id the caller has a role= FOO
@DenyAll this method is not callable by anyone
JBoss AS 7 | Gabriele Falace
Security domains
The EJB needs to know
1. Where to read users/roles definitions, against which validate
the identity of the user currently logged in.
2. The user currently logged in, itself.
The solution to (1) is given by security-domains inside the config
file standalone.xml
Each security-domain define rules for authentication and
authentication providers where credential are stored (properties
files, databases, LDAP servers, etc…)
In the following slide it is shown the default security domain
(other), which uses a couple properties files included in JBoss.
JBoss AS 7 | Gabriele Falace
Security domains
JBoss AS 7 | Gabriele Falace
Adding users
To add a user with a role, let’s use the add-user.bat script,
choosing ApplicationRealm
By doing so, the new user will be added to the properties
files referenced by the security-domain
IMPORTANT:
The EJB class has to be annotated with the security
domain: @org.jboss.ejb3.annotation.SecurityDomain("other")
JBoss AS 7 | Gabriele Falace
A secured EJB
JBoss AS 7 | Gabriele Falace
Security – the web tier
• In order to authenticate the current user with the provider
configured in the security-domain, we also have to
configure the web part
1. Add a jboss-web.xml file (in the same directory as web.xml) with
the following content
so to make the web-app aware of the security-domain
JBoss AS 7 | Gabriele Falace
Security – the web.xml
2. Add to web.xml these elements to configure authenticated access
to specified resources:
JBoss AS 7 | Gabriele Falace
Authentication methods
• Setting auth-method as BASIC provides just a basic HTTP authentication, with a pop-up asking for username and password
• The auth-method can also be a FORM and in such case we need to specify both 1. Login page
2. Error page
• The form will have action = j_security_check and parameters names will have to be j_username e j_password mandatorily
• The following slides refer to a situation in which we have a login folder in WebContent, containing both the login and the error page. Note: the /login/* path has been commented out.
JBoss AS 7 | Gabriele Falace
The FORM authentication method
JBoss AS 7 | Gabriele Falace
The login page
JBoss AS 7 | Gabriele Falace
References
• JBoss official website
http://www.jboss.org/developer/tutorials.html
JBoss AS 7 | Gabriele Falace