JBoss AS 7 - development basics


Published on

This presentation includes some configuration/development practices to get JBoss AS 7 up and running. Materials have been gathered all over the Internet, while examples, code excerpts and procedures are taken from personal hands on experience.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

JBoss AS 7 - development basics

  2. 2. JBoss AS 7 | Gabriele Falace INSTALLATION AND INITIAL SETUP
  3. 3. 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.
  4. 4. 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)
  5. 5. 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.xbinadd-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.xbinstandalone.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 )
  6. 6. JBoss AS 7 | Gabriele Falace Initial setup
  7. 7. 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.xbinjboss-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.
  8. 8. JBoss AS 7 | Gabriele Falace DATASOURCES
  9. 9. 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.Finalmodulescom and create the following directory: mysqlmain obtaining jboss-as-7.1.0.Finalmodulescommysqlmain 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
  10. 10. 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.Finalstandaloneconfiguration open the standalone.xml file Note: it is also easily accessible in Eclipse in Filesets > Configuration File under the ‘Servers’ tab.
  11. 11. 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>
  12. 12. 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>
  13. 13. JBoss AS 7 | Gabriele Falace HORNETQ AND JMS
  14. 14. 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. 2. 3. Double click on the server Under ‘General Information’ click on ‘Runtime Environment’ 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
  15. 15. 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>
  16. 16. JBoss AS 7 | Gabriele Falace An MDB processing JMS messages
  17. 17. JBoss AS 7 | Gabriele Falace A JMS sender
  18. 18. JBoss AS 7 | Gabriele Falace INFINISPAN
  19. 19. 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
  20. 20. JBoss AS 7 | Gabriele Falace INFINISPAN
  21. 21. JBoss AS 7 | Gabriele Falace CLUSTERING
  22. 22. 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.
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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.
  27. 27. JBoss AS 7 | Gabriele Falace APPLICATION SECURITY
  28. 28. 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
  29. 29. 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.
  30. 30. JBoss AS 7 | Gabriele Falace Security domains
  31. 31. 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")
  32. 32. JBoss AS 7 | Gabriele Falace A secured EJB
  33. 33. 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
  34. 34. JBoss AS 7 | Gabriele Falace Security – the web.xml 2. Add to web.xml these elements to configure authenticated access to specified resources:
  35. 35. 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. 2. Login page 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.
  36. 36. JBoss AS 7 | Gabriele Falace The FORM authentication method
  37. 37. JBoss AS 7 | Gabriele Falace The login page
  38. 38. JBoss AS 7 | Gabriele Falace References • JBoss official website http://www.jboss.org/developer/tutorials.html