Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Thursday, 30 June 2011

Books for CXF web service

Apache CXF web service Development

Do you use Apache CXF? If so, you might take an interest in "Apache CXF Web Service Development".

The book does a good job of covering CXF use cases, going beyond the usual trivial Jax-WS examples. It also covers Jax-RS (RESTful) web services, and covers each in enough detail that you're likely to find what you need when working with CXF.

Jax-WS has largely demystified basic web service development, so there's a great amount of content on the web that will show you how to quickly annotate a POJO to get a web service up and running. But what if you need to do contract-first (top down) development? Lightweight resources often conveniently bypass this more difficult trail, but this book does a good job of handling it. (This is no great accomplishment for a book on web service development, but it does set the tone for the types of things this book will show.)

It also covers restful web services. I'd say a Java developer either currently using or wanting to use Apache CXF. The book isn't a complete reference for CXF, but it does introduce all the important topics. Once introduced, there's enough content to either solve your problem or at least educate you enough to effectively research what remains.

This book can be found here.

Wednesday, 22 June 2011

HttpClient (Apache commons) code sample

I was playing around with Apache commons Http utilities the last day. I used to use the java.net.* APIs to satisfy my HTTP(s) needs.
Here is a sample code which I wrote which takes a URL as input, sets the basic request parameters (e.g. cookie string) and set the proxy settings along with the user credentials.

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientTest
{
public static void main(String[] args)
{
HttpClient client = null;
GetMethod getMethod = null;
int responseCode = -1;
byte[] responseStream = null;

String urlString = "http://www.facebook.com";
String cookieString = null;

try
{
// Creating the GetMethod instance
getMethod = new GetMethod(urlString);

// Retries to establish a successful connection the specified number
// of times if the initial attempts are not successful.
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(1, false));
getMethod.getParams().setParameter("http.socket.timeout", new Integer(5000));
getMethod.setRequestHeader(new Header("Cookie", "<COOKIE_STRING>"));

// Creating an HttpClient instance
client = new HttpClient();

// Proxy settings: Configures the proxy host, port & user
// credentials and the scope of the credentials.
client.getHostConfiguration().setProxy("<HOST>", <PORT>);
Credentials credentials = new UsernamePasswordCredentials
("<USERNAME>", "<PASSWORD>");
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
client.getState().setProxyCredentials(scope, credentials);

// Sets the user-agent for the client instance
client.getParams().setParameter("http.useragent", "<USER_AGENT>");

// Sends the GET request and gets the response
responseCode = client.executeMethod(getMethod);
responseStream = getMethod.getResponseBody();

System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: \n" + new String(responseStream));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
getMethod.releaseConnection();
client = null;
}
}
}


Note: I was looking for an API set which does make use of its own Socket level implementation. I don’t think HttpClient got it’s own implementation as it claims to be 100% Java. If you know any API set which performs better than java.net.* APIs please feel free to share it.



Wink – A framework for RESTful web services from Apache

Apache Wink 1.0 is a complete Java based solution for implementing and consuming REST based Web Services. The goal of the Wink framework is to provide a reusable and extendable set of classes and interfaces that will serve as a foundation on which a developer can efficiently construct applications.
Taken from Apache Wink official site: Click Here
Wink consists of a Server module for developing REST services, and of a Client module for consuming REST services. It cleanly separates the low-level protocol aspects from the application aspects. Therefore, in order to implement and consume REST Web Services the developer only needs to focus on the application business logic and not on the low-level technical details.
REST Web Service design structure



The Wink Server module is a complete implementation of the JAX-RS v1.0 specification. On top of this implementation, the Wink Server module provides a set of additional features that were designed to facilitate the development of RESTful Web services.
The Wink Client module is a Java based framework that provides functionality for communicating with RESTful Web services. The framework is built on top of the JDK HttpURLConnection and adds essential features that facilitate the development of such client applications.

Tuesday, 21 June 2011

Apache POI api for Java people

It is one of the great tool from the Apache,

1st of all i want to write about what is POI?,why we need POI?.

What is POI?
“Poor Obfuscation Implementation”

Why we need POI?
reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel using Java.
when i was trying to read the data from the .doc files by normal datainputstream reader , i get some garbage data along with original data.
then i startred searching for resolving this issue..at last i found that this API is very useful to work with microsoft formats.
i worked with only .doc file for  reading data line by line , in next post i will explain how to do that.
you can find more info regarding apache POI at WIKI and at apache project site .
Apache POI api  download from here.

Friday, 17 June 2011

What is map reduce?



MapReduce is a parallel programming technique made popular by Google. It is used for processing very very large amounts of data. Such processing can be completed in a reasonable amount of time only by distributing the work to multiple machines in parallel. Each machine processes a small subset of the data. MapReduce is a programming model that lets developers focus on the writing code that processes their data without having to worry about the details of parallel execution.

MapReduce requires modeling the data to be processed as key,value pairs. The developer codes a map function and a reduce function.

The MapReduce runtime calls the map function for each key,value pair. The map function takes as input a key,value pair and produces an output which is another key,value pair.

The MapReduce runtime sorts and groups the output from the map functions by key. It then calls the reduce function passing it a key and a list of values associated with the key. The reduce function is called for each key. The output from the reduce function is a key,value pair. The value is generally an aggregate or something calculated by processing the list of values that were passed in for the input key. The reduce function is called for each intermediate key produced by the map function. The output from the reduce function is the required result.

As an example , let us say you have a large number of log files that contain audit logs for some event such as access to an account. You need to find out how many times each account was accessed in the last 10 years.
Assume each line in the log file is a audit record. We are processing log files line by line.The map and reduce functions would look like this:

map(key , value) {

// key = byte offset in log file
// value = a line in the log file
if ( value is an account access audit log) {
account number = parse account from value
output key = account number, value = 1
}
}
reduce(key, list of values) {
// key = account number
// list of values {1,1,1,1.....}
for each value
count = count + value

output key , count
}
The map function is called for each line in each log file. Lines that are not relevant are ignored. Account number is parsed out of relevant lines and output with a value 1. The MapReduce runtime sorts and groups the output by account number. The reduce function is called for each account. The reduce function aggregates the values for each account, which is the required result.

MapReduce jobs are generally executed on a cluster of machines. Each machine executes a task which is either a map task or reduce task. Each task is processing a subset of the data. In the above example, let us say we start with a set of large input files. The MapReduce runtime breaks the input data into partitions called splits or shards. Each split or shard is processed by a map task on a machine. The output from each map task is sorted and partitioned by key. The outputs from all the maps are merged to create partitions that are input to the reduce tasks.

There can be multiple machines each running a reduce task. Each reduce task gets a partition to process. The partition can have multiple keys. But all the data for each key is in 1 partition. In other words each key can processed by 1 reduce task only.

The number of machines , the number of map tasks , number of reduce tasks and several other things are configurable.

MapReduce is useful for problems that require some processing of large data sets. The algorithm can be broken into map and reduce functions. MapReduce runtime takes care of distributing the processing to multiple machines and aggregating the results.

Apache Hadoop is an open source Java implementation of mapreduce. Stay tuned for future blog / tutorial on mapreduce using hadoop.

Tuesday, 21 September 2010

Arrays in java - More

  • Exchanging two elements.
  • .clone()
  • Jakarta ArrayUtils class: http://jakarta.apache.org/commons/lang/api/index.html
  • The array passed to main.
  • Class of arrays?