Java Errors and Solutions
Thursday, March 15, 2012
Unix and SQL Tips
Thursday, April 1, 2010
Debugging WebServices (under Weblogic enviroment) using Eclipse and Soap UI
Debugging WebServices using Eclipse and Soap UI:
Webservice Developers usually develop a webservice. Then for unit testing they usually prefer following approaches:
1. Creating a main/ test class which calls that service method(this service method would be the one called by the web method).
2. Testing using Soap UI.
Usually the webservices are having very less business implementation. And Hence, the input and output parameters are very less. So, there was not much need to do a line-by-line debug in webservices.
But recently, I got a chance to work on a webservice which has more than 50 parameters and accessing lot of business method inside it. Hence If I do an unit testing with SOAP UI for every small change it ate lot of my time.
So, I got a question in my mind "Why cant we do a line-by-line debug in a webservice." Once i give an hit from Soap UI it should stop in the first break point of my code and i should run through line by line to see how the values are changing in business methods.
But when i tried in Google i got only less results that too not matching to my requirement. Later in Some forums i got some suggestions related to a web-appplication debugging.
Finally, the following change attained the goal.
- First check whether you are deploying the application in ManageServers
- Then Open StartManagedWeblogic.sh.
- Add the following line after the last "set " of JAVA_OPTIONS.
set JAVA_OPTIONS=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8003 -DMS1.debug=true -Djava.compiler=NONE %JAVA_OPTIONS%
The only important thing you should note here is the 'address' and 'debug' parameter.
set the address = A port in the running server which is not in use
set the debug to true with the manage server name appended as
"-D
Wednesday, March 24, 2010
StringBuilder - An Efficient String Concatinator
We know Strings are immutable, so each time a string is changed, a new instance in memory is created.
But StringBuffer and StringBuilder can change their values.
The only difference between StringBuffer and StringBuilder is:
StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer for single threaded operations.
Criteria to choose among String, StringBuffer and StringBuilder:
- If your text is not going to change use a string Class because a String object is immutable.
- If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
- If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
Some Test Results:
For 1000000 String Concatination:
StringBuffer elapsed time in milliseconds: 348 , 314 , 320 , 311
StringBuilder elapsed time in milliseconds: 220 , 226 , 225 , 245
But for String for 10000 String concatination: It took 4471 , 4384 milliseconds. Bcs On the ( + ) operation a new String object is created at each iteration
Wednesday, February 24, 2010
Smart Way of Doing Null Checks for JAXBElements
I got a situation like : To track the data passed to a webservice, i need to create a large string which has values of the number of inputs passed to a webmethod.As all the input elements are JAXB elements and most of the input elements are optional, I must have to do a null check to each and every element before converting it to string.For this i created a small method as follows:
private String JAXBNullHandler(JAXBElement obj) { if (obj != null) { return String.valueOf(obj.getValue()); } else { return "NULL"; } }Wherever i need the string value of an element i used the method call as follows:
JAXBNullHandler(amount); where amount is a JAXBElement
This has reduced a considerable amount of code and the code is legible.
Monday, February 8, 2010
Remember this before using Spring Context Loading
My aim is to persist all types of errors occuring in webservices. Hence i have created an helper class and injected the helper class in webservice Impl class as follows:private SaveExceptionHelper saveExceptionHelper;
String[] paths = { "/spring/service.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
saveExceptionService = (SaveExceptionServiceImpl) ctx.getBean("saveExceptionService");
Also in service.xml i have put
Upto this I didnt get any problem in build and deployment.
Later i have used a DAO which is having persitance method of hibernate.
and injected in service.xml under SaveExceptionServiceImpl.
I got an exception like
" abc.def.User class not found"
Where "User.class"" is created in some other project folder and imported in SaveExceptionServiceImpl.
Later i fixed it by using an interface to the helper class as saveExceptionService = (SaveExceptionService) ctx.getBean("saveExceptionService");
And i removed the import of ServiceImpl class.
Moral of the story is:
- The Importance of using Interfaces in Spring context initialization.
- The helper classes which has implemenation should not use resources from other projects. Incase, it is using other resources, we need an interface for that class and inject the interface in webservice class. Impl class in context loader xml(service.xml).
Thursday, January 21, 2010
Error Occurs in saving List of beans and Displaying in table
- A jsp page with a like of Item Details populated in a table.
- User will modify any of the data in any of the column.
- After submitting the jsp the updated details needs to be saved in database
we are able to display in screen. But the List is set to null if we submit the same form again.
The list iteration is like below:
I have searched in net for the solutions. But I couldn't find any thing. Hence I have to go for a hard solution as below:
- Once after the list is displayed in Jsp, in the Onclick of Submit button, I have called a javascript:function.
- The list of data displayed is retrieved using the javascript by browsing through the tags inside the jsp.
- The retrieved data is converted into a string buffer with "," appended for each column values and ";" appended for each rows.
- After creating the string buffer, It is set to a variable in a form( say hiddedDataList).
- Finally, the variable with the string is set as hidden variable using html:hidden.
- We can again tokenize the string from the variable and create a list and save in the same list variable of form.
Monday, January 18, 2010
Persisting a BLOB in hibernate mapping
record in a table
"ORA-01465: invalid hex number" While using BLOB in hibernate:
I have to convert a data object to a BLOB and save it in a data base.
I was facing an error called "ORA-01465: invalid hex number " whenever i tried to save the data row with the blob data in one of the column.
Error created code piece.:
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
try {
message = oracle.sql.BLOB.createTemporary(null, false, oracle.sql.BLOB.DURATION_SESSION);
message.setBytes(1, ba.toByteArray());
System.out.println("This is the Blob message " + message);
} catch (SQLException e) {
LOG.error("Error while converting Byte array to BLOB ", e);
}
os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();
}
}
Later I found in one of the site that there is a class called BlobImpl in Hibernate. I used following code to resolve the issue.
Solution:
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
// message = oracle.sql.BLOB.empty_lob();
// message.setBytes(1, ba.toByteArray());
BlobImpl blobImpl = new BlobImpl( ba.toByteArray() );
message = blobImpl;
// System.out.println("This is the Blob message " + message);
os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();
}