Monday, December 24, 2012

Ways to Start Your JBoss Instance

Hello There,

JBoss User's, you must be knowing the ways to start the JBoss Application Server by going into its bin directory and invoking standalone.bat or domain.bat (.sh for Unix)

Recently I found a beautiful utility for starting server which is equivalent to WebLogic's "java weblogic.Server command, however JBoss ones look little different.

For this you need to define the JBOSS_HOME for the Jboss Installation directory in Environment Variables of your local system (Windows), for Unix and Linux Systems you may need to set it and include it PATH variable.

Consider my settings are on Windows and have added the JBOSS_HOME in Environment variables of my Windows 7 Operating System.



After this open a command shell and navigate to your JBoss Installation Directory.



You would see below files in the current directory


C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1>dir
 Volume in drive C is OSDisk
 Volume Serial Number is E604-B574

 Directory of C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1

21/11/2012  22:25    <DIR>          .
21/11/2012  22:25    <DIR>          ..
21/11/2012  22:24    <DIR>          appclient
21/11/2012  22:24    <DIR>          bin
21/11/2012  22:24    <DIR>          bundles
21/11/2012  22:24             2,451 copyright.txt
21/11/2012  22:24    <DIR>          docs
21/11/2012  22:24    <DIR>          domain
21/11/2012  22:24           266,549 jboss-modules.jar
21/11/2012  22:24            26,530 LICENSE.txt
21/11/2012  22:25    <DIR>          modules
21/11/2012  22:24             2,421 README.txt
26/11/2012  15:09    <DIR>          standalone
21/11/2012  22:25    <DIR>          welcome-content
               4 File(s)        297,951 bytes
              10 Dir(s)  157,544,517,632 bytes free

C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1>

Here we will just try to start the standalone serverby invoking the org.jboss.as.standalone you will be able to find all the available modules in C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1\modules\org  so that we can try different tasks to invoke and implement.

Just Invoke the below command : 

java -jar jboss-modules.jar -mp "%JBOSS_HOME%\modules"  org.jboss.as.standalone -Djboss.home.dir="%JBOSS_HOME%" on the command line.

Press Enter


20:37:15,557 INFO  [org.jboss.as.naming] (MSC service thread 1-6) JBAS011802: Starting Naming Service
20:37:15,605 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-4) JBAS015400: Bound mail session [java:jboss/mail/Default]
20:37:16,949 INFO  [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-3) Starting Coyote HTTP/1.1 on http--127.0.0.1-8
080
20:37:16,986 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 40) JBAS010403: Deploying JDBC-com
pliant driver class org.h2.Driver (version 1.3)
20:37:20,446 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) JBAS010400: Bound data source [java:jbo
ss/datasources/ExampleDS]
20:37:21,453 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-2) JBoss Web Services - Stack CXF Se
rver 4.0.2.GA
20:37:24,254 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-2) JBAS015012: Started FileSystemDeploymentServic
e for directory C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1\standalone\deployments
20:37:24,299 INFO  [org.jboss.as.remoting] (MSC service thread 1-5) JBAS017100: Listening on /127.0.0.1:9999
20:37:24,299 INFO  [org.jboss.as.remoting] (MSC service thread 1-8) JBAS017100: Listening on /127.0.0.1:4447
20:37:26,666 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
20:37:26,669 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 32884ms - Started
133 of 208 services (74 services are passive or on-demand)


Let me explain the command.

java -jar jboss-modules.jar -> Here we are executing the jboss-modules.jar this Jar file is needed to bootstrap the application server Kernel based on the JBoss Modules present in Modules folder.

When we invoke the standalone.sh file or the domain.sh file it will internally does the same Job as WebLogic Server is built.

now let us see the complete command line.

java -jar jboss-modules.jar -mp "%JBOSS_HOME%\modules"  org.jboss.as.standalone -Djboss.home.dir="%JBOSS_HOME%"

-mp : its known for Module Path, we are directing the command to get the modules specified in the command from the Directory specified for this parameter.

And we are specifying the JBoss Home Directory to just guide the command at the right location.

Make sure you provide correct details for the path and the variables specified here otherwise you have chances to get the errors like below.


C:\Users\shailesh.dyade\SandBox\ApplicationServers\jboss-as-7.1.1>java -jar jboss-modules.jar -mp "%JBOSS_HOME%\modules" org.jboss.
as.standalone
java.lang.IllegalStateException: JBAS015816: Missing configuration value for: jboss.home.dir
        at org.jboss.as.server.ServerEnvironment.<init>(ServerEnvironment.java:325)
        at org.jboss.as.server.Main.determineEnvironment(Main.java:242)
        at org.jboss.as.server.Main.main(Main.java:83)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.jboss.modules.Module.run(Module.java:260)
        at org.jboss.modules.Main.main(Main.java:291)


Reason : If you do not specify the home directory.

Please try by yourself and let me know if you find any difficulties to start the JBoss Server Instance.

Regards
Shailesh Dyade.


Friday, December 21, 2012

Configure Eclipse With JBoss Application Server

Hello There

Today we will look into configuring Eclipse to Work with JBoss Application Server

Prerequisites

1) You need to have Eclipse (Java and J2EE compliant package) copied in your local.
2) Of course JBoss copied in your desired location of your local system.

Next Steps:
==========

Step 1) Start Eclipse and Go to Tools - > Install New Software


Step 2) Click on Add after the Text Box for Work with

You will get a Pop-Up Window as below.

       

Add Name as : JBoss Tools  & Location as below
http://download.jboss.org/jbosstools/updates/development/indigo/

You will see window as ->

Step 3) Wait for Some time after that you will get a big list of JBoss Tools and Utilities which are supported in Eclipse.


From there you need to expand the first one : Abridged JBoss Tools 3.3 and Select the check box of JBossAS Tools 

And Say Next. 

Step 4) Eclipse will check for the Dependencies and report in case if the tools is conflicting with any of the existing modules. 

Step 5) In case if you find any conflicts, its your headache to resolve it :), you will not get any unless you have fresh Eclipse Installation.

Step 6) Proceed for the Installation - Say Next.

Step 7) Do not forget to accept the License Agreement from JBoss.. 


Step 8) Say Finish.!! - You will see the installation has started for Tools you have selected.


     

After Installing You may need restart Eclipse, so the below Pop-up.

      

Step 9) After this you will be able to see JBoss Application Server in the Server Run-time List for Eclipse - Go to Platform Runtime Window and click on new server wizard.



Step 10) To Configure the Server Successfully, Click Next on Above Window and Provide accurate details,  for where your server is installed and the JRE location you would like to use and say Finish.

Step 11) Right Click and Start the Server or You can click on Green Play Button for Starting the Server.

In Console you should notice : 

JBAS015875: JBoss AS 7.1.1.Final "Brontes" started (with errors) in 19726ms - Started 126 of 208 services (7 services failed or missing dependencies, 74 services are passive or on-demand)

Step 12) Time to celebrate now.. your server is started and you will see Fully Loaded Eclipse.. :)




Hope you have liked the Posting, Stay Tuned.!! Lot more to come.

Bye for now.!!

Regards
Shailesh Dyade

Wednesday, December 19, 2012

Developing And Hosting Application on Tomcat Application Server

Hello There,

Have you ever tried developing code and running it over the tomcat being an Administrator of Middleware Technologies, if not you should do it once to learn how things work at very low level, this posting is for start-up Administrators, who would like to learn how the application coding is done, how the applications are deployed and how application servers take the charge.!!

Tomcat as by itself an application server, contains the Web Container and the EJB Container, which host the  JSP, Servlets and EJB Specific Applications.

Here at first place we will configure the Tomcat Application Server with Eclipse IDE and will develop a sample application in Eclipse in deploy it over the Tomcat.

Configuring Tomcat With Eclipse.
============================

Note : As a pre-requisite, you need to have tomcat installed in your local machine ( I am considering it as Windows here)

1) Open Eclipse IDE (Download the Eclipse from this Link)



2) Go to Java EE Perspective as Shown in the picture above at the Right top corner of the Window.

3) You will see Servers tab at the bottom in Platform Runtime Window (To Understand about the Eclipse you need learn about that first ( I am not so good though..;))

4) Click on new server wizard link.

5) You will see a below window popped up in a flash ( Unless you have Core i5 Processor)

6) As I have installed Tomcat 7.x version, I will select the same, you can select your own from the list.

7) Click On Finish ( If you Click on Next, you will have different option to Add resources to the Server environment)

8) You will see the Eclipse window as below

You have a Tomcat Server Configured in the Platform Runtime Window and There is a new Project got created as  "Servers" in Package Explorer Window. If you expand that, it will look like the one below.

You can edit the files and save the files from Eclipse environment from now.

9) Well on other way you have option to start and stop the Tomcat Server from within of Eclipse IDE, Just Right Click on server name and Click Stop/ Start

10) And if you would like to verify you can very well test the page from browser.

http://localhost:9090/ (My Champ (JBoss) is Running on 8080 and asking for the reason to free it up..!!)

There are no files as there are no resources added to the tomcats work directory.


Developing and Deploying Application through Eclipse
==============================================

1) Lets create a Dynamic Web Application in Eclipse.

Go to New -> Dynamic Web Content -> Name Your Application as below - Say Finish

2) You will see the hierarchy of the project as below, its because you have selected the Dynamic Web Content for the project type.

3) Now Create the Web Content - Right Click on WebContent Node -> New -> JSP File

4) After you create this file you will see by default there will be some JSP Specific code will be generated in the file as below.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
##### JSP CODE GOES HERE #####
</body>
</html>

You just need to write your JSP Code between the <body></body> tags.

Copy the below code between <body></body> tags as below.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<form action="HelloServlet">
Please enter a color <br>
<input type="text" name="color"size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>


Change the title as per your convenience. 

5) If you see, its calling needs a servlet to do get and post the values from JSP's Form which we have see in the above code, lets create it.

6) Right Click on Java Resources : src -> New - Servlet as below.

7) Name your class as HelloWorld as below.

8) Place the below code in the file. ( you may need to remove all other extra things which is already generated, or else you can reuse the same for extra coding purpose ;)


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException 
 {
   // reading the user input
   String color= request.getParameter("color");    
   PrintWriter out = response.getWriter();
    out.println ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
    out.println("<html>\n");
       out.println("<head> \n"); 
       out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"> \n");
       out.println("<title>Hellow World JSP</title> \n");
       out.println("</head> \n");
       out.println("<body> \n");
       out.println("<font size=\"12px\" color=\"" +color+ "\">"+"Hello World From Shailesh Dyade");
       out.println("</font> \n");
       out.println("</body> \n");
       out.println("</html>");   
 }  
}

Though you can change the code as you wish to develop the application ;)

9) Create web.xml page for accessing these pages - Right Click on WebContent -> New -> File -- File Name : web.xml


















Once created; copy the below contents to the file and Save it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
</web-app>

10) Now you have to test and Run the Application - As we have already configured the Tomcat Server Engine, go to Platform Runtime Window and Right Click on Tomcat Server Name and Go to Add Remove Project.



















11) From Available Projects Add the JSPServletExample Project in the Configured List - Say Finish.

12) Start the Tomcat Server by Right Clicking on it and verify the Tomcat Applications.

13) Try to Access the Page for your Application at 

http://localhost:9090/JSPServletExample/ and verify the file HelloWorld.jsp available in the list.

Now Access the below link or Click on the File HelloWorld.jsp

http://localhost:9090/JSPServletExample/HelloWorld.jsp

14) And See the Page : 














15) Try to Enter a Color in the Text Box and Hit Submit - Here my fav.. Red Wins...!!










Hope you have enjoyed the tutorial, See you later with a new concept and a tutorial soon..!!

Regards
Shailesh Dyade.

How to Fix Memory Leaks In Java

Well Guys, I do not have much to do on this posting as I have nothing to, but you have.!!

Refer the below link to know more about Memory leaks and How to Fix them.

How to Fix Memory Leaks In Java

Regards
Shailesh Dyade

Friday, November 30, 2012

Tomcat Clustering - Apache WebServer

Hello There,

Today early morning I tried something new, which I never tried to do it but always wanted to, had a new assignment to learn tomcat (http://tomcat.apache.org) and clustering tomcat, finding and adding up a little to the statement - "pressure makes man perfect", I thought what is there to learn.?, bunch of configuration files and batch files, Google it and you will be settled to live with this guy.!, did the same and was able to see my tomcat running with hosted applications (example application) now I need to bring another exceptionally performing mini software to work with him called Apache (http://www.apache.org/) to work as frontend help desk for the processor behind. Let's see how are we able to establish this.!!

Steps To Follow
-------------------

1) Download and Install both Softwares

  • Apache - http://mirror.reverse.net/pub/apache/httpd/binaries/win32/httpd-2.0.64-win32-x86-no_ssl.msi
    • Or you can manually go the site http://httpd.apache.org/download.cgi and download desired versions from desired mirrors.
    • Need Admin Permission to Install.
    • I tried it on httpd-2.0.64-win32-x86-no_ssl.msi
    • I have used localhost for the hostname and admin@localhost as email id
  • Apache Tomcat - http://tomcat.apache.org/download-70.cgi
    • Download any version that suits your requirement
    • I tried it on apache-tomcat-7.0.32
    • Just Unzip file and you are done with installation.
  •  Verify the setup details and test the installation by testing the default application after starting them.
  • For More Information refer : 
    • http://httpd.apache.org/docs/2.0/platform/windows.html
    • http://tomcat.apache.org/tomcat-5.5-doc/setup.html#Windows
  •  Done

2) Configure Tomcat for Clustering - ( Vertical Clustering) 

For Clustering we need to have at least 2 instances of the server instances and within that we need to have load balancing established, for creating another instance of tomcat just copy the tomcat folder, paste in the same directory and rename it.

C:\SandBox\WebServers\TomcatCluster>dir
 Volume in drive C is OSDisk
 Volume Serial Number is E604-B574

 Directory of C:\SandBox\WebServers\TomcatCluster

23/11/2012  17:58    <DIR>          .
23/11/2012  17:58    <DIR>          ..
23/11/2012  17:58    <DIR>          apache-tomcat-1
23/11/2012  17:58    <DIR>          apache-tomcat-2
               0 File(s)              0 bytes
               4 Dir(s)  175,658,151,936 bytes free


few of the configurations we need to change to enable the tomcat to work for other server ( to establish cluster)

modify the server.xml file present in apache-tomcat-x/conf/ as below for both instances.

apache-tomcat-1
---------------------
1) Change the port number as below.

<Server port="8105" shutdown="SHUTDOWN">

2) Change the Port Number as below for HTTP Protocol.


<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3) Enable the AJP Protocol if it is disabled and change the port number 

<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />

4) Enable the Cluster feature for Tomcat by disabling the comments as below.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

5) Open the Application for which you would like to test the Clustering Feature and add the <distributable/> within <web-app></web-app> tags..............

I have tested it for examples webapp under <CATALINA_HOME>/webapps/ (CATALINA_HOME) is another name for Tomcat Home - you need to specify this in system's environment variables.

Example : Go to C:\SandBox\WebServers\TomcatCluster\apache-tomcat-1\webapps\examples\WEB-INF


<servlet-mapping>
      <servlet-name>wsSnake</servlet-name>
      <url-pattern>/websocket/snake</url-pattern>
    </servlet-mapping>
    <distributable/>
</web-app>

Now repeat the steps for apache-tomcat-2 instance.

apache-tomcat-2


1) Change the port number as below.

<Server port="8205" shutdown="SHUTDOWN">

2) Change the Port Number as below for HTTP Protocol.


<Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />


3) Enable the AJP Protocol if it is disabled and change the port number 

<Connector port="8209" protocol="AJP/1.3" redirectPort="8443" />

4) Enable the Cluster feature for Tomcat by disabling the comments as below.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

5) Open the Application for which you would like to test the Clustering Feature and add the <distributable/> within <web-app></web-app> tags..............

I have tested it for examples webapp under <CATALINA_HOME>/webapps/ (CATALINA_HOME) is another name for Tomcat Home - you need to specify this in system's environment variables.

Example : Go to C:\SandBox\WebServers\TomcatCluster\apache-tomcat-2\webapps\examples\WEB-INF


<servlet-mapping>
      <servlet-name>wsSnake</servlet-name>
      <url-pattern>/websocket/snake</url-pattern>
    </servlet-mapping>
    <distributable/>
</web-app>



Restart both Tomcat Instances.

3) Configure Apache for Clustering with Tomcat

Hope you have already installed Apache WebServer and tested on http://localhost/

Configuring Apache Server involves below steps :


  • Downloading mod_jk library
    • Please download the mod_jk.so file from link
    • once you get the zip file, unzip it and copy the mod_jk.so file to Apache2/modules folder.
  • Configure mod_jk.so file in Apache Configuration
    • Create mod_jk.conf file with following entries
# Load mod_jk module

# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile C:/SandBox/WebServers/apache2.0.64/Apache2/conf/workers.properties

# Where to put jk logs
# Update this path to match your logs directory location
JkLogFile C:/SandBox/WebServers/apache2.0.64/Apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

# Send everything for context /examples to worker ajp13
JkMount /examples loadbalancer
JkMount /examples/* loadbalancer
    • Add following entry in httpd.conf file.
include C:/SandBox/WebServers/apache2.0.64/Apache2/conf/mod_jk.conf
  • Create worker.properties for handling requests from Apache and copy the below content to the file.
    • You can specify the tomcat home for first tomcat instance
# Define 1 real worker named ajp13
workers.tomcat_home=C:/SandBox/WebServers/TomcatCluster/apache-tomcat-1
workers.java_home=%JAVA_HOME%
worker.list=node1,node2,loadbalancer

# Set properties for worker named ajp13 to use ajp13 protocol,
# and run on port 8009

worker.node1.port=8109
worker.node1.host=localhost
worker.node1.type=ajp13
worker.node1.lbfactor=1

worker.node2.port=8209
worker.node2.host=localhost
worker.node2.type=ajp13
worker.node2.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=node1,node2
worker.loadbalancer.sticky_session=1
  • Copy the file worker.properties file to Apache2/conf directory.
  • Restart Apache Server.
3) Access the URL : http://localhost/examples or deploy any session replication enabled application to test whether session is getting replicated or the clustering load balancer is working properly or not.














Additionally if you see the mod_jk.log file, following entries are noticed during this operation.


[Fri Nov 30 15:02:16 2012]loadbalancer localhost 2.102120
[Fri Nov 30 17:07:40 2012]loadbalancer localhost 0.442025
[Fri Nov 30 17:07:40 2012][8720:6044] [info] ajp_send_request::jk_ajp_common.c (1619): (node2) all endpoints are disconnected, detected by connect check (1), cping (0), send (0)
[Fri Nov 30 17:07:40 2012]loadbalancer localhost 0.235014
[Fri Nov 30 17:14:07 2012]loadbalancer localhost 0.101006


Hope I haven't missed anything yet.!!

Regards
Shailesh Dyade