Pages

Showing posts with label J2EE. Show all posts
Showing posts with label J2EE. Show all posts

Monday, April 22, 2013

Creating simple JMS application, understand JMS with basic example, JMS queue example

I hope you already have a basics of the JMS concpet, a bit more about it in different way.

1. when to use JMS or when a test case or secanrio to eligible for use JMS?
    1. When you want to decouple an application  and keep it loosly coupled with
    other systems/modules.
    2. When your application is growing and has growing responsibility to maintain
    interaction with modules.
    3. While communicating with other system there has to be no need of waiting for response
    immideatly, I would rather say that if you design application to communicate with other system
    asyncronously.
in all above case it is preferred to use JMS as a way for communiation protocol.
these are few examples where JMS can be used efficiently...
1. in your saving account you have sms alert on transactions.
2. you submit your tax processing appliction.
3. request for checkbook order
4. request for transaction details report (that will be emailed)
5. monitoring system which audits data based on events.

I would also like to share that there will be more different exampls which may look like can be build
only with syncronus communication, but they can be designed differently in such a way to work with JMS
as to improve performance of the system and scalability.

Lets take an example of banking application now.

I have a banking application which has integral module within that sends email and sms
on transaction processiing. now bank is devleloping another module which is a pesonal
goal module where use specify his saving goal and once it is achieved it will send sms
there also new module need to be added about alter of new bills payment that customer have to pay.

now think that if the banking application was desingned earlier in such a way that this small
sms/email module was developed as a integral part of system and as more and more functionality
gets added its become difficult to maintain and provide different features/ integrate with different
module. (this is a simple scenario presented here don't think of too much of complexity or alternatives)

lets try to design it as a loosly coupled system.

assume that bank has a independant module deployed on the server along with banking application
and both communicate over JMS because banking appliation do not need any feedback after it post
message for sending email or sms, rather confirming customer that request processed.

OK now who is consumer and who is producer?, what type of JMS messaging it is?
in this case your sms/email module is a consumer of the message as it process message for sending email or sms.
and your bank application is a producer of the message who send the request to process message.
and this is queue based message processing because response (sms/email) is intended to one recepient only.

Lets try to create a sample application both bank and sms module and deploy on weblogic server
here I am using weblogic as a JMS server hope you know that weblogic provides JMS Server functionality also.
else you can use different server as well, but concept is same.

Step 1. setting up of JMS Server on weblogic

1. click on jms server >> summery of jms server>> new
2. type the name of jms server you desired, persistance store: either select if you have one or create new
3. persistance store is of two type file and jdbc, select file store as persistance store, which means your messages are
persisted in file. >>next
4. enter the name for file store, target is admin server since it is not cluster or managed server, also make of note
given on sever for directory
*The pathname to the directory on the file system where the file store is kept.
*This directory must exist on your system, so be sure to create it before completing this tab.
so create directory and enter the path. >>next
5. Console will come back to same page of step 3 select file store and click next.
6. select jms server target and click finish.

Setp 2 set up jms module

1.from the top again go to summery of jms services >>jms module>> new
2. enter name of the module,description, location in domain can be left blank, >> click next
3.select target as admin server>> next
4. select add resources to jms module >> click next.

step 3. Setup Connection Factory and Queue

in last step of 2 will create module and take you to resources page where you can define jms queue now.

1. Click new >> select Connection factory >> click next
2. Enter jms factory name and jndi name… that is only important now. subscription policy exclusive, clientid policy unrestricted
max message 10, xa conn.. unchecked
3.click next >>and select adminserver>> next/finish
4.You are back again on summary of resources page Click again on new >> select queue option >> next
5. Enter the queue name and its jndi name, template none>>click next
6.Click on create new subdeployment>> Enter the subdeployment name>>ok
7. select subdeployment >>target will be shown previously created jms server select and >>finish

with this we are finish with setting up jsm server and queue now we will create java provider class

I have attached the screen shots and sample example as link here.
jms-screenshots.doc
jms-screenshots.docx
BankAppSMSSender.java >> is a simple jms producer which generated message and post it on server
SMSProcesssor.java >> is the consumer which reads message from jms server and process it.

Soon I will write on them more...
















Monday, April 8, 2013

javax.servlet.ServletException: java.lang.NoClassDefFoundError: javelin/jsp/JspFunctionMapper

Recently I come across above error on weblogic 12c migration project, since  for my test I used fexisting old ear that was built for weblogic 10.0.2 version. Later I found that this error has caused a problem because application is using pre-compiled jsps inside, I also noticed that weblogic 10.3.0  this jsp compilation has been depricated. so the solution was to remove the pre-compiled jsp classes from your war file or compile them again using weblogic 12c, I took a shortcut to remove classes and guess what, it failed again, since you must also know that who wants to use pre-complied jsps also use JSPClassServlet servlet to make sure that
weblogic at runtime should not try to recompile them and must always use pre-compiled. So that was additional task for me to remove this servlet and its mapping from web-inf to make sure my shortcut way should work.


Tuesday, August 31, 2010

creating JAX-RPC web service using weblogic workshop

Hi, Please find this attached word document explaining steps for creating JAX-RPC webservice
using weblogic workshop.
weblogic is extending the eclipse module so it will be more similar if screen shot if you have only eclipse.
I have added steps of creating weblogic domain and deploying of webservice on weblogic server as well.
if you are using apache Axis then it will be bit different. I may add this later.

follow this link to view steps

Wednesday, August 25, 2010

JNDI Basics

Java Naming and Directory Interface (JNDI)

Naming and Directory Concepts:
Naming Concepts :
In computer system every program that we write, create files we gave them a name to identify. so giving name to a object helps to lookup into system.
so nameing service typically map object with user friendly name to its address into the system. These objects are used by a program in system.
for e.g. the Internet Domain Name System (DNS) maps machine names (such as www.sun.com) to IP addresses (such as 192.9.48.5). A file system maps a filename (for example, c:\bin\autoexec.bat) to a file handle that a program can use to access the contents of the file.
To look up an object in a naming system, you supply it the name of the object. The naming system determines the syntax that the name must follow. This syntax is sometimes called the naming system's naming convention.
For example, the UNIXTM file system's naming convention is that a file is named from its path relative to the root of the file system, with each component in the path separated from left to right using the forward slash character ("/"). The UNIX pathname, /usr/hello, for example, names a file hello in the file directory usr, which is located in the root of the file system.

Bindings:
The association of a name with an object is called a binding. For example, a file name is bound to a file.

The DNS contains bindings that map machine names to IP addresses. An LDAP name is bound to an LDAP entry. (Lightweight Directory Access Protocol)

References and Addresses:
Depending on the naming service, some objects are stored directly and some are not, objects which are stored directly are mapped to its address and those are not
are mapped as references i.e. pointer to its location. in naming service. a reference gives information about how to access an object.

Context:
A context is a set of name-to-object bindings. Every context has an associated naming convention. A context provides a lookup (resolution) operation that returns the object and may provide operations such as those for binding names, unbinding names, and listing bound names. A name in one context object can be bound to another context object (called a subcontext) that has the same naming convention.
like a DNS domain, such as COM, is a context. A DNS domain named relative to another DNS domain is a subcontext. For example, in the DNS domain Sun.COM, the DNS domain Sun is a subcontext of COM.

Naming Systems and Namespaces:
A naming system is a program that stores set of contexts of the same type (they have the same naming convention) and provides a common set of operations. \
for e.g. DNS provides naming service of mapping name to ip address, LDAP provides naming service of mapping name to Directories and files etc.
A namespace is the set of names in a naming system.



JNDI Overview
The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM programming language. It is defined to be independent of any specific directory service implementation. Thus a variety of directories--new, emerging, and already deployed--can be accessed in a common way.
The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services
The JNDI is included in the Java 2 SDK, v1.3 and later releases.
To use the JNDI, you must have the JNDI classes and one or more service providers. Java 2 SDK, v1.3 and later releases aleredy includes following
service provider classes in it

  • Lightweight Directory Access Protocol (LDAP)
  • Common Object Request Broker Architecture (CORBA) Common Object Services (COS) name service
  • Java Remote Method Invocation (RMI) Registry

Other service providers can be downloaded from the JNDI Web site or obtained from other vendors

The JNDI is divided into five packages:

* javax.naming
* javax.naming.directory
* javax.naming.event
* javax.naming.ldap
* javax.naming.spi


References:

for more details and tutorial in depth please have a look at following link

sun tutorial for JNDI


sample Naming example from sun site

This example shows you how to write a program that looks up an object whose name is passed in as a command-line argument.


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

class Lookup {
public static void main(String[] args) {
// Check that user has supplied name of file to lookup
if (args.length != 1) {
System.err.println("usage: java Lookup ");
System.exit(-1);
}

String name = args[0];

// Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");

try {

// Create the initial context
Context ctx = new InitialContext(env);

// Look up an object
Object obj = ctx.lookup(name);

// Print it out
System.out.println(name + " is bound to: " + obj);

// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.err.println("Problem looking up " + name + ": " + e);
}
}
}

Friday, June 26, 2009

Getting started with Ajax: Basics.

Background:

I had used XmlHttp in my javascripts for quite long and extensively. espically dealing with grid control... but lately I come to know that it was called as an AJAX... which has much more than that... to make your UI richer. A common way of calling ajax function is to create an object of XMLHttpRequest. this you can either use it directly or can use wrapper libraries available in the market like extjs, JQuery, Prototype etc. to begin with I will present the internal coding of actual ajax code how it works. please see the following code...

Basic Code: First to check which browser we are using and accordingly activate XmlHttp object as



function GetXmlHttpObject(){
var xmlHttp=null;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

It just that I have saparated code. creating of XMLHttp object in different function as above and calling it in other function as below


function getAjaxResponse(sURL){
xmlHttp = GetXmlHttpObject();
xmlHttp.onreadystatechange=stateChanged(){
if (xmlHttp.readyState==4){
document.getElementById(divID).innerHTML=xmlHttp.responseText;
};
};
xmlHttp.open("GET",sURL ,true);
xmlHttp.send(null);
}

Lets observe the code above we have specifed call back function to receive response from server.



xmlHttp.onreadystatechange=stateChanged();

... here you can either mention function name or you can inline the function itself. this function block will get executed when you receive response from server. Here actually you can do more based on the status code that you receive from server, for e.g. you can show process status while you receive response or please wait messgage/image to user till response received etc. please see the table below for the status code that you get on this function.
Response that you get from the server can be of Html code or xml or simply a string, and it depends how you want to show it to user. in above code I have shown that it returns html response which I directly paste into div block.
fire http GET/POST request to server


xmlHttp.open("GET",url,true);
xmlHttp.send(null);

Please note the following things when calling open,

  • Always set Async to true.
  • always set call back function before calling send()
  • To send form data/POST use endcodeURI() for all data values and set mime type of request to "application/x-www-form-urlencoded"

basic advantage of using javascript to post the request to server is that we don't have to wait till server response arrives. instead we can perform other task. I am listing below the XMLHttpRequest objects method available here.


Method/ PropertyDescription.
abort()stops listining for current response.
getAllResponseHeader()Get array of response header as key value pair.
getRespoonseHeader(name)Get response header by name passed as key.
open(method, url[,asyncFlag[, "username"[, "password"]]])

This prepares the xmlHTTPRequest object to make call to the server.
Method: HTTP; can be either GET, PUT or POST.
url: is url string to open can be either obsolute or relate may include querystring

send(content)

perform http request.
content: string to be used as request body, i.e. to form POST body, you can format your POST parameters just like querystring.

setRequestHeader(header, value) Add header to Http requset, normally a key value pair and mostly used to set content type of request.
onreadystatechangeused to set as callback fucntion that handles request state change.(similar to onclick, onload)
readyState

returns the status of the request which can be on of following at a time
0:= uninitialised
1:= loading
2:= loaded
3:= interactive
4:= complete

responseTextreturns server response as String
ResponseXMLreturns server response as XML Document
statusreturns HTTP status code like 200 for OK etc
statusTextreturn String representation of HTTP status code like OK for 200, not found for 404 etc.

In above example I have already shown how I am getting HTML response from server which I am setting back to perticlular html object. see below the example of parsing xml response received from server and setting to html object.
here I assume that I am getting some key-value pair from server as xml response which i need to set to combobox in html so I am passing combobox object to method

xmlHttp.onreadystatechange=usrfunc;
....
....
function processListXML(xmlId, objlist ){
// first remove all existing option values from list box
for(i=objlist.options.length-1;i>=0;i—){
objlist.remove(i);
}
var t = document.getElementById(xmlId);
var tableData = t.getElementsByTagName("RList");
if(tableData != null){
var t = tableData.item(0);
var columns = t.getElementsByTagName("ROption");
if(columns != null){
for (j = 0; j < columns.length; j++){
key= columns[j].getAttribute("id");
value= columns[j].firstChild.data;
objlist.add(new Option( value ,key ));
}
}
}
}


Now assume that I get following xml
<RList>
<ROption id="01">INDIA </ROption>
<ROption id="02">JAPAN </ROption>
<ROption id="03">AMERICA </ROption>
</RList>