Skip to main content

Posts

Showing posts with the label Java

Parsing mailing address in Java

Recently came across a problem to parse postal address String using Java, did some research here are some findings. Given String s = "60 Strawberry Hill Ave Stamford CT 06901" Required Result Street = Strawberry Hill Ave City = Stamford Number = 60 zip = 06901 state = CT You can use  JGeocoder public static void main(String[] args) { Map<AddressComponent, String> parsedAddr  =     AddressParser.parseAddress("Google Inc, 1600 Amphitheatre Parkway, Mountain View, CA 94043"); System.out.println(parsedAddr);       Map<AddressComponent, String> normalizedAddr  = AddressStandardizer.normalizeParsedAddress(parsedAddr);         System.out.println(normalizedAddr); } Output will be: {street=Amphitheatre, city=Mountain View, number=1600, zip=94043, state=CA, name=Google Inc, type=Parkway} {street=AMPHITHEATRE, city=MOUNTAIN VIEW, number=1600, zip=94043, state=CA, name=GOOGLE INC, type=PKWY} There is ano...

Clean and Simple Email Sender Design

Recently review design for an email sender with following requirements: Build a service to send email Service must have failover recovery, in youcase if system goes down and comes back up it should still send out pending email reminders You are not expecting any acknowledgements for the messages sent. The design proposal in place was to use a funky framework with persistence store etc etc.  After going through the simple requirements stated above, used following clean and simple design and I am happy to say we made right decision: Use Java Mail API to send email reminders Create a table which stores reminders to be sent and update a flag after email reminder is sent In case system goes down, you can resend all reminders for which flag was not set. It was so simple and easy to implement and we have all control in our hand and there was no RAMP up time to learn new frameworks. So, if you are confused with n number of ideas available out there using x numbe...

Map Reduce Simplified

Yes it is about parallel and distributed computing, there are tonnes of web pages, books articles, diagrams etc. etc with nice buzz words to talk about Map Reduce, here is the most simplified explanation. Lets take a real life example. 1. Company CEO called all Program Manager's "I need total effort spent this month by noon". Program Manager's no problem sir. Why are Program Manager's not worried because they are going to distribute task :-) 2. Each Program Manager called their project manager asking for effort spent so far. 3. Each Project Manager pulled up effort sheet and provided it to their Program Managers. 4. Program Managers complied received sheet into one file and sent it to CEO. 5. Company CEO collated all the sheets and calculated total effort spent. Each individually broke its task to smaller tasks (Mapped its input task to smaller tasks), Program Manager was required to provide effort spent, he mapped his task to smaller tasks, this is...

Apache Roller Getting Started

If you are thinking to setup a blog website then apache roller is the quickest and simplest thing to do so, below are steps for getting started, though these instructions are present in documentation provided by apache, but hopefully following steps will make it very easy for you. Here you Go !! 1. Download apache roller http://roller.apache.org/download.cgi#roller50 2. Install Tomcat http://tomcat.apache.org/download-70.cgi 3. Download mysql http://dev.mysql.com/downloads/ 4. Unzip apache roller zip file and copy roller-5.0.1-tomcat.war from \roller-weblogger-5.0.1-for-tomcat.zip\roller-weblogger-5.0.1-tomcat\webapp to tomcat webapps folder 5. Create mysql database, dont worry about schema, that will be automatically created later. mysql -u root -p password: ***** mysql> create database rollerdb; mysql> grant all on rollerdb.* to scott@'%' identified by 'tiger'; mysql> grant all on rollerdb.* to scott@localhost identified by 't...

View Drools Generated Code

When we execute rules, behind the scene java code is generated which is actually getting executed, to see this java code you need to set configuration path where you want java code generated by drools to be stored. PackageBuilderConfiguration configuration = new PackageBuilderConfiguration(); configuration.setDumpDir(new File("XYZ")); final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory .newKnowledgeBuilder(configuration); Set XYZ as a directory path where you would like drools to store generated code. e.g. XYZ = "c:/drools/codegen" When you run your program the java classes generated for the .drl files are stored at the path XYZ. This generated code is really helpful to understand the behavior in lot of situations.

JSON Optimizing for Faster Rendering

All web applications have fast rendering as one of the primary goals, JSON is a preferred choice in most of AJAX based applications because it is lightweight data interchange format. Consider a scenario where you have to read data from database and render it on UI, typically the steps which will be followed are: a. Send request data to server using AJAX calls b. At server end invoke the Business layer c. Business layer calls the database layer d. Data returned from database layer is populated into business objects e. From business objects build JSON objects, send them back to client f. Client use Javascript libraries to render data on UI. If everything works fine for you, you can stop reading right now, because you are achieving the required goal in the best way. But in real world this might not be working for you because of performance issues at one or the other step mentioned above. Here are few performance tips, they might suite in some of the scenarios and might be th...

Drools - An overview

For Java based applications the most challenging part has always been the business logic maintenance, and pick any applications which you find complex and if we ask ourself how complex it would be moving forward, the answer will always be nX times. What do we do ? Drools comes for Rescue as a Rule Engine. Drools provides mechanism: a. To write business logic in simple english language b. Easy to maintain and very simple to extend c. Reusability of logic by defining keywords in a DSL file and using them in DSLR file. But be careful nothing comes free, everything takes cost in terms of memory and time space. Use Drools if you really have : a. Business logic which you think is getting cluttered with multiple if conditions because of variety of scenarios b. You will have growing demand of increase in the complexity c. The business logic changes would be frequent (1 - 2 times a year would also be frequent) d. Your server's have enough of memory as it is a memory hungar...

Class Data Sharing

Class data sharing (CDS) a feature introduced in J2SE 5.0 reduces the startup time for Java programming language applications. When the JRE is installed on 32-bit platforms using the Sun provided installer, the installer loads a set of classes from the system jar file into a private internal representation, and dumps that representation to a file, called a "shared archive".Class data sharing is not supported in Microsoft Windows 95/98/ME. During subsequent JVM invocations, the shared archive is memory-mapped in, saving the cost of loading those classes and allowing much of the JVM's metadata for these classes to be shared among multiple JVM processes. The primary motivation for including CDS in the 5.0 release is the decrease in startup time it provides. CDS produces better results for smaller applications because it eliminates a fixed cost: that of loading certain core classes. The smaller the application relative to the number of core classes it uses, the larger the sav...

Apache AXIS2 Web Service Client

This article covers details about writing a web service client using Apache AXIS2 API. The article explains the various API using code snippets and covers parameters require by various methods of web service client API. Apache AXIS2 web service API mainly consists of two type of objects ServiceClient and OperationClient. ServiceClient provides basic APIs to send and receive SOAP messages, for advanced methods you require Operation Client. To provide target URL you need to define a EndPointReference type of object. EndpointReference targetEPR = new EndpointReference(http://localhost:8080/axis2/services/helloworld); Various options can be given to web service client by creating an Options object. The reference point defined is set into the options along with the Transport protocol. Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); Now create a service client and pass these options to service client usi...

Deploying Web Service in Apache AXIS2

The purpose of this article is to talk about how to write web service and deploy it in Apache AXIS2. First step is downloading and installing apache axis2. There are two ways to use Apache AXIS2. 1. Install AXIS2 as a standalone server using the Standard Binary Distribution. 2. Deploy axis2.war and deploy it in a servlet container. We will be using the 2nd method in the article. To check the installation open the url http://localhost:8080/axis2 and you should be able to see the home page of apache axis2. Please locate the folder axis2\WEB-INF\services in exploded war file of axis2. This is the folder where you will copy the code of your service and it will get deployed in apache axis2. Lets us take a simple example. Write your service class. public class HelloWorld { public String sayHello(String firstName, String lastName) { System.out.println("Hello World Invoked"); MessageContext incomingContext = MessageContext.getCurrentMessageContext(); FileDataSourc...

Getting Started with Apache Lucene

Apache Lucene is a high-performance, full-featured text search engine library. The API of lucene is very simple to use. Here is overview of some of the objects required to start using Apache Lucene. Document and Fields The class "org.apache.lucene.document.Document" is necessary container for the index. Lucene requires all indexed objects to provide an instance of Document. Each document defines one or several fields ( (org.apache.lucene.document.Field). Fields contain classified information about the document or metadata related to document. A sample classification is for example the creation date of a file, author of the document etc. These fields allow you to search later for a specific information in this classification. IndexWriter The class org.apache.lucene.index.IndexWriter creates the index. Via the method addDocument you can add an existing Document to the index. The constructor for IndexWriter expects the directory to store the index, and the analyzer f...

Calling Struts Action by Javascript

To call a struts action by Javascript on an event, you need to write a function which is invoked when the event occurs and this function makes an AJAX call. The AJAX call can send a request to the same URL which is provided in action attribute of form tag or html:form tag. e.g. In case your action URL is something like "/registerUserAction.do?action=forgetPasswordForward" that you provide in action attribute of html:form, you can pass the same url to the mentioned code snippet below: function makeRequest(url, callbackfunction) { var http_request = getXMLHttpObject(); url = url + "isComingFromAjax=true"; if (url.indexOf('?') != -1) { url = url + "&" ; } else { url = url + "?" ; } url = url + "isComingFromAjax=true"; http_request.onreadystatechange = callbackfunction; http_request.open('POST', url.substring(0, url.indexOf('?')), true); http_request.setRequestHeader('Content-Type...