Total Visitors

Sunday, March 16, 2014

Spring 4.0 MVC Example

Here I will show you how to create a simple example by using Spring MVC.
I will be creating this example without the help of xml, i.e whole configuration is done using pure Java.
Just copy paste the content and you yourself google it out to understand the code.
Note* this application is working fine with GlassFish applicaiton server.

1) Instead of web.xml write the class Initializer.java 

package com.spring.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);

ctx.setServletContext(servletContext);

Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}

2) Instead of servlet-context.xml

package com.spring.init;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration //Specifies the class as configuration
@ComponentScan("com.spring") //Specifies which package to scan
@EnableWebMvc //Enables to use Spring's annotations in the code
public class WebAppConfig {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}

3) Define your controller class.

package com.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringController {
@RequestMapping(value="/hello-page")
public ModelAndView goToHelloPage() {
ModelAndView view = new ModelAndView();
view.setViewName("hello"); //name of the jsp-file in the 'page' folder

String str = "Finally Spring 4.0 Working !";
view.addObject("message", str); //adding of str object as 'message' parameter

return view;
    }
}

4) hello.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <p>Hello world: ${message}</p>
    <p>Nice!</p>

5) index.jsp (Run this and Enjoy!)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<h1>Home page</h1>
<p>This is a Home Page.</p>
<p><a href="hello-page.htm">Hello world link</a></p>

Saturday, December 7, 2013

Clustering and Load Balancing in JBoss 7.1 using Apache mod_jk

Comming Soon ....

Auto Connection after Network Failure in JBoss 7.1

Put the following inside <datasource> .... </datasource> :- 
  <validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>false</background-validation>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
  </validation>
<timeout>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>1</idle-timeout-minutes>
</timeout>

----- OR -----

<validation>
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>false</background-validation>
</validation>
<timeout>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>5</idle-timeout-minutes>
</timeout>

----- OR -----
<validation>
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>false</background-validation>
</validation>


----------EXPLAINATIONS-------------

--> <valid-connection-checker-class-name> :- a class that can check whether a connection is valid using a vendor specific mechanism

--> <check-valid-connection-sql> :- an sql statement that is executed before it is checked out from the pool (see <validate-on-match>) to make sure
it is still valid.If the sql fails, the connection is closed and new ones created. Also it will be used by <background-validation>

--> <validate-on-match> :- whether to validate the connection when the JCA layer matches a managed connection (i.e. when the connection is checked out
of the pool). With the addition of <background-validation> this is not necessarily required.
Note: Specifying "true" for <validate-on-match> is typically not done in conjunction with specifying "true" for <background-validation> as
this would be overkill in most scenarios.  Default is true.

--> <background-validation> :- In JBoss 4.0.5 background connection validation was added to help reduce the overall load on the RDBMS system  when validating a connection. When using this feature, JBoss will attempt to validate the current connections in the pool is a separate thread (ConnectionValidator). This must be set to true for <background-validation-minutes> to take effect.  Default is false.

--> <stale-connection-checker-class-name> :- An implementation of org.jboss.resource.adapter.jdbc.StateConnectionChecker that will decide whether
SQLExceptions that notify of bad connections throw org.jboss.resource.adapter.jdbc.StateConnectionException (from JBoss5)

--> <exception-sorter-class-name> :- a class that looks at vendor specific messages to determine whether sql errors are fatal and thus the connection  should be destroyed.  If none specified, no errors will be treated as fatal.

--> <blocking-timeout-millis> :- the length of time to wait for a connection to become available when all the connections are checked out. Default is 30000 (30 seconds).

--> <idle-timeout-minutes> :- indicates the maximum time a connection may be idle before being closed. Setting to 0 disables it.  Default is 15 minutes.


Sunday, November 24, 2013

What is AJP? A short introduction



  • AJP is used to communicate between the web server and the servlet container
  • The Apache JServ Protocol (AJP) is a binary protocol that can proxy inbound requests from a web server through to an application server that sits behind the web server. The web server is a “reverse proxy,” meaning, its purpose is to handle incoming traffic from the Internet on behalf of the application server.
  • Gal Shachor was the original designer of this protocol. There is, apparently, no current documentation of how the protocol works.
  • Apache JServ is a 100% pure Java servlet engine fully compliant with the JavaSoft Java Servlet APIs 2.0 specification
  • Web implementers typically use AJP in a load-balanced deployment where one or more front-end web servers feed requests into one or more application servers.
  • AJP runs in Apache HTTP Server using the mod_jk plugin.
  • When it was developed the goals of this protocol was
    • Increasing performance (speed, specifically).
    • Adding support for SSL, so that isSecure() and getScheme() will function correctly within the servlet container.
  • A binary format was presumably chosen over the more readable plain text for reasons of performance.

Monday, November 18, 2013

Enabling Debug Facility in JBoss7 AS.

Go to bin/standalone.conf.bat of JBoss 7 AS:-

Inside this file just remove the rem from the below given line:

rem # Sample JPDA settings for remote socket debugging
set "JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

Then if you are using eclipse as an IDE do the following steps:-
1) Go to Debug configuration
2) Go to Remote Java Application
3) Assign Port: 8787
4) Give your desire Project name and Enjoy!

Wednesday, November 13, 2013

Migration From JBoss6 to JBoss7 AS Servers

1) Make a lib dir inside the EAR and copy paste only the required.(Removed)
Place all your depended lib inside the EarContent/lib folder.
2) Go to Eclipse and make add the lib classpath from this EarContent/lib folder.
3) Here in SRS it will give jxl error, so give classpath of this specific jxl jar file which is situated inside common.


1) ERROR: Caused by: org.jboss.msc.service.ServiceNotFoundException: Service service jboss.ejb.default-resource-adapter-name-service not found
solution: inside standalone.xml add the following lines under "<subsystem xmlns="urn:jboss:domain:ejb3:1.2">"
in standalone.xml:-
<mdb>
  <resource-adapter-ref resource-adapter-name="hornetq-ra"/>
  <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>
Note:
If you are configuring it inside standalone.xml but inside standalone-full.xml it is not required and no error will be displayed.

2) Create Connection Pooling.
Go to Datasource and make a connection pooling for your database.
In JBoss 7 configuration the JNDI path is different:
private final String GLOBAL = "java:global";
private final String EJB ="SRSEJB";
Its: GLOBAL + EksConstants.FILE_SEPARATOR+ EAR_NAME + EksConstants.FILE_SEPARATOR +EJB+ EksConstants.FILE_SEPARATOR + jndiName;
--Goto persistance.xml and do the following:
<jta-data-source>java:jboss/SRSOracleDS</jta-data-source>

3) Caused by: java.lang.NoClassDefFoundError: jxl/format/CellFormat
-- https://community.jboss.org/thread/195522
That location isn't meant for placing the application jars. You'll have to package the jxl.jar within
the EarContent/lib folder
That's what the EE spec says about application libraries.

4) Switch to standalone-full.xml, here you don't have to explicitly write step:1

5) Go to standalone.conf.bat and standalone.conf file and change standalone.xml to respective .xmls.

6) In JB7 the @WebContext has changed the jar path. Earlier it was import org.jboss.wsf.spi.annotation.WebContext;
Now it has changed to import org.jboss.ws.api.annotation.WebContext;

7) As in JBoss6 go to your hornetq-jms.xml and pick up:
<jms-queue name="SRSScheduleQueue">
<entry name="/queue/SRSScheduleQueue"/>
</jms-queue>
and paste it inside the standalone-full.xml of JB7 under  "<jms-destinations>"

8)
Go to JBoss Admin Console and add your property configuration in System Properties.
Or add properties at run time manually by CLI(Command Line Argument) as shown below:-
 As in JBoss6 go to your properties-service.xml and pick up:
JBOSS_CONF_FOLDER=C:/jboss-6.1.0.Final/server/conf
SRS_PROP=SRS.properties
RESOURCE_PROP=resources.properties
paste it in a notepad and save it as a .properties file in a drive (say D:/).
-- Prepare a bat file as shown below:
c:
cd C:\jboss-as-7.1.1.Final\bin
standalone.bat --properties d:\srsConfig.properties
Run it...
Observation:-
After load the properties, u can see it at
localhost:8080 > Configuration > Environment Properties
Note:-
Run it......

9) Set your mail configuration inside standalone-full.

Hot Deployment in JB7

#Steps:- https://community.jboss.org/message/723945

1. Please make sure to add
<configuration>
<jsp-configuration development="true"/>
</configuration>
in standalone.xml under <subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" native="false">        

2. Go to jboss-as-7.1.1.Final/modules/org/jboss/as/web/main
  Place the jboss-as-web-7.1.1.Final-RECOMPILE.jar in there. You can download from http://www.datafilehost.com/download-2cb9ff04.html

3. Open module.xml (jboss-as-7.1.1.Final/modules/org/jboss/as/web/main) and add the following line
<!--resource-root path="jboss-as-web-7.1.1.Final.jar"/-->
<resource-root path="jboss-as-web-7.1.1.Final-RECOMPILE.jar"/>

-----------------------------------OR ANOTHER WAY!-----------------------------

<servlet>
            <servlet-name>jsp</servlet-name>
            <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
            <init-param>
                <param-name>development</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>3</load-on-startup>
        </servlet>