My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Saturday, March 8, 2014

Host Identifier and Spring

Spring and Host Identifiers

 
   
 
            
            
             Dev
            
    

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HostIdentifier {
    @Autowired
    private String hostname;

    @Autowired
    private String region;

    public String getHostname() {
 return hostname;
    }

    public void setHostname(String hostname) {
 this.hostname = hostname;
    }

    public String getRegion() {
 return region;
    }

    public void setRegion(String region) {
 this.region = region;
    }

}

TimeStamp Adapters for JAXB

As we have seen in many cases, the Timestamp coming in as Sting, it is easier to cast it using JAXB.

Here is the TimeStampAdapter which we can use it to streamline the same

Just define the TimeStamp attribute of the JAXB Object as date as follows
    @XmlElement(name = "ActionTimestamp")
    @XmlJavaTypeAdapter(TimeStampAdapter.class)
    private Date actionTimestamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class TimeStampAdapter extends XmlAdapter {
    private SimpleDateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private SimpleDateFormat secondTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");

    @Override
    public String marshal(Date v) {
 return firstFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws ParseException {
 Date returnDate = null;
 try {
     returnDate = firstFormat.parse(v);
 } catch (Exception prse) {
     returnDate = secondFormat.parse(v);
 }
 return returnDate;
    }
}

Date and Time Adapter for JAXB

It is often very time consuming to check for the incoming String and cast it using SimpleDateFormat in every class. Here is a simple way to fasten this by using Date Adapters in JAXB itself.

All you need to do is to define the JAXB objects to be Date as follows

@XmlElement(name = "CreditValueDate")
    @XmlJavaTypeAdapter(DateTypeAdapter.class)
    private Date creditValueDate;
    @XmlElement(name = "DebitValueDate")
    @XmlJavaTypeAdapter(DateTypeAdapter.class)
    private Date debitValueDate;

Class for DateTypeAdapters

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateTypeAdapter extends XmlAdapter {
    private SimpleDateFormat firstDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat secondDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    @Override
    public String marshal(Date v) {
 return secondDateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws ParseException {
 Date returnDate = null;
 try {
     returnDate = firstDateFormat.parse(v);
 } catch (Exception prse) {
     returnDate = secondDateFormat.parse(v);
 }
 return returnDate;
    }
}

Convert Object to XML String

 public String convertObjectToXmlString(ObjectClass theObjectClass)
   throws Exception {
  Marshaller triggerMarshaller = null;
  System.out.println("Converting String into XML");
  /** Anonymous Inner Class */
  OutputStream output = new OutputStream() {
   private StringBuilder string = new StringBuilder();

   @Override
   public void write(int b) throws IOException {
    this.string.append((char) b);
   }

   @Override
   public String toString() {
    return this.string.toString();
   }
  };

  JAXBContext jc = JAXBContext.newInstance("Your Package Name");
  triggerMarshaller = jc.createMarshaller();
  triggerMarshaller.marshal(triggerMessage, new StreamResult(output));
  return output.toString();

 }

Saturday, February 15, 2014

Wrap Method parameters in new line in IDE


Here is an alternative to format method params as you wish
1. Windows--> Preferences-->Java -->Code Style-->Formatter
2. Under the profile name even default choose the tab Line Wrapping
3. Image shown below for various options

Adjust settings for CTRL + SHIFT + F in eclipse


In many a situations, your code will be line wraps after 80 characters by default.
You may want to change it for readability purpose.
Here is an alternative to change this settings in your eclipse.

1. Windows--> Preferences-->Java -->Code Style-->Formatter
2. Select Java Conventions [Built-in] from the Active Profile
3. Click on Edit
4. Go to the TAB named “Line Wrapping”.
5. Under General Settings  , change the Maximum Line Width fro, 80 to your number
6. Under the preview section, Set Line Width for preview window from 80 to your number
7. You may have to rename the profile as MyProfile_Java_Profile since built ins are READ-ONLY
Now go to your source and press CTRL + SHIFT +F , you will see that all line breaks are after your number

Thursday, February 6, 2014

Mule to return multiple rows from a JDBC query as a single transaction


Mule to return multiple rows from a JDBC query as a single transaction 


Option -1
 
  
  
  


Option -2
Setting transactionPerMessage to 'false' on the connector should work if you ALSO set transactionPerMessage property to 'true' on the endpoint and add a transaction like so