Pages

Friday, May 22, 2009

Web Service Quick Start

 

What are Web Services?
Web services are application components
Web services communicate using open protocols
Web services are self-contained and self-describing
Web services can be discovered using UDDI
Web services can be used by other applications
XML is the basis for Web services

How Does it Work?
The basic Web services platform is XML + HTTP.

XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.

The HTTP protocol is the most used Internet protocol.

below are Web services platform elements:

SOAP (Simple Object Access Protocol)
a framework for exchanging XML-based information in a network
It defines a uniform way of passing XML-encoded data.
In also defines a way to perform remote procedure calls (RPCs) using HTTP as the underlying communication protocol.

UDDI (Universal Description, Discovery and Integration)
provides a registry mechanism for clients and servers to find each other
uses SOAP for communication

WSDL (Web Services Description Language)
an XML-based language for describing network services
WSDL descriptions of capabilities and locations of services
like an interface description language for Web services
communication using SOAP or direct HTTP

Web services uses set of tools that can be used in a number of ways. The three most common styles of use are RPC, SOA and REST.
Remote procedure calls
RPC Web services present a distributed function (or method) call interface that is familiar to many developers.
Typically, the basic unit of RPC Web services is the WSDL operation
The first Web services tools were focused on RPC, and as a result this style is widely deployed and supported. However, it is sometimes criticized for not being loosely coupled, because it was often implemented by mapping services directly to language-specific functions or method calls. Many vendors felt this approach to be a dead end, and pushed for RPC to be disallowed in the WS-I Basic Profile.

Service-oriented architecture
Web services can also be used to implement an architecture according to Service-oriented architecture (SOA) concepts, where the basic unit of communication is a message, rather than an operation. This is often referred to as "message-oriented" services.

SOA Web services are supported by most major software vendors and industry analysts. Unlike RPC Web services, loose coupling is more likely, because the focus is on the "contract" that WSDL provides, rather than the underlying implementation details.

Representational state transfer
Representational State Transfer (REST) attempts to describe architectures which use HTTP or similar protocols by constraining the interface to a set of well-known, standard operations (like GET, POST, PUT, DELETE for HTTP). Here, the focus is on interacting with stateful resources, rather than messages or operations. An architecture based on REST (one that is 'RESTful') can use WSDL to describe SOAP messaging over HTTP, which defines the operations, can be implemented as an abstraction purely on top of SOAP (e.g., WS-Transfer), or can be created without using SOAP at all.

 

Further to understand how RPC model can be defined using the following steps:

1. A client application builds an XML document containing the URI of the server that will service the request, the name of the remote method to execute, and the parameters associated with that method.
2. The targeted server receives and unwinds the XML document. It then executes the named method.
3. After the named method has returned its results, the results are packed into a response XML document, which is sent back to the calling client.
4. The client application receives the response and unwinds the results, which contains the response of the invocated method.

Web Services tools with Java
The most essential Java tools for Web service development:
xml.apache.org/axis - Apache AXIS (Apache Extensible Interaction System)
a Java-based implementation of SOAP+WSDL
largely allows the programmer to forget these technologies  typically used together with Tomcat

www.alphaworks.ibm.com/tech/ettk - alphaWorks's EETK (Emerging Technologies Toolkit)
support for SOAP, WSDL, UDDI and much more...

java.sun.com/webservices/downloads/webservicespack.html - Sun's Java WSDP (Web Services Developer Pack)
support for SOAP, WSDL, UDDI, ...
JAX-RPC maps SOAP/WSDL to RMI (Java Remote Method Invocations)

imp links
Tutorial on what is web service
http://www.w3schools.com/webservices/default.asp

Typical soap RPC based web service example
http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html

List of web service framework on which web service can be built
http://en.wikipedia.org/wiki/List_of_Web_service_Frameworks

Wikipedia definition and detail note on web service
http://en.wikipedia.org/wiki/Web_service

Top Ten FAQs for Web Services
http://webservices.xml.com/pub/a/ws/2002/02/12/webservicefaqs.html

Some Notes on Coding

some of the points you should note while doing development

1. on java

  • Use primitive data types instead of objects as instance variables.
  • Use the final modifier on instance-variable definitions to create immutable internally accessible objects
  • Eliminate unnecessary casts (use jdk1.5 feature)
  • String.equals() is expensive if you are only testing for an empty string. It is quicker to test if the length of the string is 0.
  • When using Vector, ensure that elementAt() is not used inside a loop.
  • Modify java.lang.String to cache the hashCode if you are using many string keys in hash tables [note Sun added this optimization to the String class in SDK 1.3]
  • Reorder CLASSPATH so that the most used libraries occur first
  • Compile java files with the optimizer on.

 

2. On JDBC

  • Turn off autocommit, but don't leave transactions open for too long.
  • Cache any required metadata and use metadata methods as rarely as possible as they are quite slow. Use Connection.setReadOnly(true) to optimize read-only
  • Choose the optimal cursor: forward-only for sequential reads; insensitive for two-way scrolling. Avoid insenstive cursors for queries that only return one row.
  • Use a parametrized remote procedure call (RPC) rather than passing parameters as part of the RPC call, e.g. use Connection.prepareCall("Call getCustName (?)").setLong (1,12345) rather than Connection.prepareCall("Call getCustName (12345)")
  • CachedRowSet provides cached result sets that do not require continuous connection to the database, allowing connections to be reused more efficiently.
  • Use PreparedStatements to batch statements for optimal performance.

 

3. On JSP/Servlet

  • Avoid using the SingleThreadModel interface for servlets: write thread-safe code instead.
  • ServletRequest.getRemoteHost() is very inefficient, and can take seconds to complete the reverse DNS lookup it performs.
  • OutputStream can be faster than PrintWriter. JSPs are only generally slower than servlets when returning binary data, since JSPs always use a PrintWriter, whereas servlets can take advantage of a faster OutputStream.
  • Do not store large object graphs in javax.servlet.http.HttpSession. Servlets may need to serialize and deserialize HttpSession objects for persistent sessions, and making them large produces a large serialization overhead.
  • Call HttpSession.invalidate() to clean up a session when you no longer need to use it.
  • For Web pages that don't require session tracking, save resources by turning off automatic session creation using: <%@ page session="false"%>
  • Timeout sessions more quickly by setting the timeout or using session.setMaxInactiveInterval().
  • Use the include directive <%@ include file="copyleft.html" %>where possible, as this is a compile-time directive (include action is a runtime directive).
  • Use the jspInit() method to cache static data, and release them in the jspDestroy() method.

web performance: have you tried this?

DEFER Your Scripts

DEFER is a relatively obscure attribute of the script element, but the performance-minded page author can use it to indicate to Internet Explorer 4.0 or later that the script tag contains no immediately executing code that impacts the document before the load event fires. Combined with the SRC attribute, the DEFER attribute can be used on a script tag to indicate to Internet Explorer that the associated code should be downloaded in the background while the rest of the components of the page are downloaded and parsed.

<SCRIPT DEFER>
function test1()
{
   // function which is not required at time page load/start  immediately

}
</SCRIPT>

 

Author Hyperlinks Consistently

The Internet Explorer cache is case-sensitive. That means that you should author your hyperlinks with case-sensitivity in mind. Take the following example.
<A  HREF="/workshop/author/dhtml/reference/dhtmlrefs.asp">DHTML References</A>
<A HREF="/Workshop/Author/DHTML/Reference/DHTMLRefs.asp">DHTML References
Both hyperlinks refer to the same page. Or do they?
On a UNIX system these hyperlinks might refer to two distinct pages; thus, Internet Explorer treats them distinctly by making separate requests to the server, allowing the server to decide how to resolve the request.

By authoring your hyperlinks consistently, you'll be saving space in the user's cache, and you'll be reducing the number of times Internet Explorer has to request the same resource from the server.

Close Your Tags

Unlike XML, HTML has the notion of implicitly closed tags. This includes frame, img, li, and p. If you don't close these tags, Internet Explorer renders your pages just fine. If you do close your tags, Internet Explorer will render your pages even faster.

It is tempting to author in the following way.

<P>The following is a list of ingredients.
<UL>
<LI>flour
<LI>sugar
<LI>butter
</UL>But the following will be parsed more quickly because it is well-formed and Internet Explorer doesn't need to look ahead to decide where the paragraph or list items end.

<P>The following is a list of ingredients.</P>
<UL>
<LI>flour</LI>
<LI>sugar</LI>
<LI>butter</LI>
</UL>

 

Leverage the HTTP Expires Header

The expires header is part of the HTTP 1.0 specification. When an HTTP server sends a resource such as an HTML page or an image down to a browser, the server has the option of sending this header and an associated time stamp as part of the transaction. Browsers typically store the resource along with the expiry information in a local cache. On subsequent user requests for the same resource, the browser can first compare the current time and the expires time stamp. If the time stamp indicates a time in the future, the browser may simply load the resource from the cache rather than retrieving the resource from the server.

Even when a resource would advertise an expiration date still to come, browsers?including Internet Explorer 4.0?would still perform a conditional GET to determine that the version of the object in the cache was the same as the version on the server. Upon careful analysis, the designers of Internet Explorer determined that this extra round trip was neither optimal nor necessary. For that reason, the behavior of Internet Explorer 5 has been modified in the following way: if the expiry of a cached resource is later than the time of the request, Internet Explorer will load the resource directly from the cache without contacting the server. Sites using the expires header on commonly used but infrequently updated resources will experience lower traffic volumes, and customers using Internet Explorer 5 will see pages render more quickly.

 

how to make sure updated .js files gets refreshed on client machine without clearing temp folder?

normally js files used in web application, are cached in clients machine and suppose if we next release
with some updation to same file on server, client browser will not come to know this change.
So we have to clean its temporary folder or clear browser cache by going into internet option>> tools >> clear brwoser history button explicitly.

but how to avoid this? how to make sure that every time we release or update .js files on server, the newer copy or updated copy gets loaded in clients browser?

here is the way
<script type="text/javascript" src="./javascript/MyScript.js"></script>
this is the normal way we use script tag which
we are going to replace as

<script type="text/javascript" src="./javascript/MyScript.js?v=14-05-2009at16:11:19"></script>
include date and timestamp with filename so that next time it will have different date and time
and it will not find same in cache so that it will load new one....

Monday, May 18, 2009

Replication... small note

What is Replication



In Simple words, Replication is the simply process of copying data from one location to another.In MySQL database server, Master is main server from which data to be copied and Slave is the server on which data copied.Replication is asynchronous - means your replication slaves do not need to be connected permanently to receive updates from the master,depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.
Some of the most common reasons for using replication are described as follows:
Performance :
This is where user can access shared data with different server, which means distributing load from main server, and also improves performance of the main system
Separating Data Entry and Reporting: Working in an environment in which the same database is used for data entry and reporting,you probably know that things aren't always rosy. sometimes you may see that tables are locked for transaction updates, or some of costly reporting queries slow down database performance byusing its resources, Replication provides great help for these issues in which translations are fired on main database (master)while reports are being separated to run on replicated database (slave), which internally improves performance at both endsfor transactions as well as reports.(this solution is best suited if you are not using reports which requires real time data of few sec/min ago or so)
Providing High Availability: Occasionally, you might consider using replication for high availability; that is, to replicate transactions from the main server to a standby server. If the main server fails, you can then point your data sources to the standby server.Replication does not provide any sort of automatic fail-over by itself.Be aware that using replication for high availability takes careful planning and testing.
Mass deployment: Replication can be used to distribute data over multiple regional locations. Then, applications can access various regional servers instead of accessing one central server. This configuration can reduce network load dramatically.
Reduce Data loss to backup: Replication doesn't mean backup of the data. but certainly it can be an alternative to backup data to minimize data lossReplication is the only technology that can satisfy the needs of the most demanding systems, as only replication can provide instant access to data and zero data loss (minimal Recovery Point Objective or RPO and minimal Recovery Time Objective or RTO). Backup is still the cornerstone of a solid disaster recovery strategy, but since backups are usually run only once a day there is a high risk of losing large amounts of data and also having systems being offlinefor long periods of time (the Recovery Point Objective and Recovery Time Objective for backup is usually days, whereas with replication it is usually minutes or seconds).

As we know, backup process always requires data locks on database server while in process (so that no data loss happens) and so it is difficult to take database backup from online transactional database during day (which cause data loss in failure)but with replication we can reduce this problem in a way that

1 main database is free, and need not to be locked during backup process (which gives high avaibility 24x7)

2 Replication gives exact snapshot of main database and so can be used by backup process to take backup instead of connecting to main server

3. Replication gives choice to use as alternate system in fail over or recover data from (slave/backup ) during system down.



imp links for further reading
http://msdn.microsoft.com/en-us/library/aa164940(office.10).aspx

Why Use Replication?

http://www.ameinfo.com/48329.html
for details of implementation and usage in MySQL

http://dev.mysql.com/doc/refman/6.0/en/replication.html

replication on Linux tutorial

http://librenix.com/?inode=8118