Tuesday, October 7, 2014

Creating Simple Weblogic JMS Queues

In this post, we'll be seeing how to create a simple JMS queue in Weblogic application server. Based on the queues created in this tutorial, new tutorials will be posted in the future on how to listen and send message to a JMS queue.

1.  Steps to create a JMS Server




4.  Create JMS Queues and Connection Factory

4.1    Create Connection Factory

    Author: Thiagu

Thursday, January 3, 2013

Passing JavaBean Datasource to JasperReports

As the title suggests the tutorial is about passing javabean as a datasource to jasper reports instead of a db connection. To make it work, create a POJO class as below with two variables.

 Than create a generic arraylist of Person and pass it into JasperFillManager as a datasource using JRBeanCollectionDataSource object. Sample code as below.


In the jasper file, just create two String fields as below which has the exact name of the variables in the POJO class.

Run the main file and the output will be as below:


Author: Thiagu

Thursday, December 9, 2010

Reading and Writing Properties

 

As the title suggests, this post is all about creating and accessing a property file through Java codes. To carry out such activities, 4 important classes have to be used and those are File, Properties, FileInputStream and FileOutputStream.

 

image

Pic P1: Shows method to write to a .properties file

Pic P1 shows how to write properties to a .properties file. Now lets explore this code line-by-line

  1. Line 26 tries to access a file on the specified file path.
  2. Line 27 checks if the file exists on the specified file path.
  3. Line 28 creates a new file if the file does not exist using createNewFile() method.
  4. Line 35 assigns properties with key and value for the selected file using setProperty() method.
  5. Line 36 writes the properties to the .properties file using store() method.

 

image

Pic P2:  Shows method to read from a .properties file

  1. Line 49 creates an Input Stream to read from a specific file
  2. Line 50 loads the .properties files using the selected Input Stream
  3. Line 54 extracts the key and key value using the get() method.
  4. Line 56 The Input stream is closed once the stream is not in use.

 

image

Pic P3: Shows sample code to test read and write methods

 

image

Pic P4: Shows output of the code in P3

 

Author: Thiagu

Wednesday, September 29, 2010

How to Get VSS files via Java Code?

Most of the time, to access VSS we’ll either use Command Line arguments or some build scripts from Java. Now, this is actually possible through java codes by using ANT APIs, which does the command line process in the background.

In this post I’m using an example on how to get files from VSS (check-in and check-out will be more or less the same) using ANT API. To make it possible, you need to make use of the “org.apache.tools.ant.taskdefs.optional.vss” package which contains classes to access VSS. Class “MSVSSGET” and some methods of its super class “MSVSS” will be used to carry out the Get process.
Let me explain the code fragment line-by-line:

  1. setLogin – This method is from the abstract class MSVSS used for authentication purpose. The String parameter has to be formatted as “username,password”.
  2. setSsdir – Is also from the class MSVSS which specifies the location of SS.EXE (VSS executable file).
  3. setRecursive – This method is from MSVSSGET class which enables to retrieve all sub-projects of the project specified in VSS path
  4. setProject – This is a method from the superclass ProjectComponent, used to specify a project for the VSS activity.
  5. setServerpath – A method from MSVSS class used to specify the VSS repository’s path (folder which contains the file called srcsafe.ini)
  6. setTaskName - A method from superclass Task used to assign a task name for the VSS activity.
  7. setDescription – A method from superclass ProjectComponent used to specify the description of the activity.
  8. setLocalpath – Method from the class MSVSSGET to assign the location on where to store the files retrieved from VSS.
  9. setVsspath – Method from superclass MSVSS used to assign the project path within the VSS repository. (e.g $/YourProject).
  10. execute – method from the superclass MSVSS used to run the command line argument to execute SS.EXE
For further understanding on the “org.apache.tools.ant.taskdefs.optional.vss” package, kindly refer to this site:

Reference:
Author: Thiagu

Tuesday, August 10, 2010

Setting Initial value for InputDialog in JOptionPane

There are 6 showInputDialog methods in JOptionPane class and we can select either one of these methods based on our requirements. To create an initial value when an InputDialog pops out we need to make use of method with these signatures:
public static Object showInputDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon,
Object[] selectionValues,
Object initialSelectionValue)
throws HeadlessException
Let me describe the parameters in a more understandable way compared to the ones in the Java API Doc.
  • parentComponent - the parent Component for the dialog
  • message – The message to display when the dialog pops out
  • title – The title of the dialog
  • messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
  • icon – Your very own icon for the dialog
  • selectionValues – Provide few values to select (If this is null, the input field will be a JTextField rather than a JComboBox)
  • initialSelectionValue – The default initial value for the dialog


If you look at the code above, I made the parentComponent as null because the dialog is not referring to any component and I’ve declared the icon null as well because I don’t need a customized icon.

When I declare the selectionValues with an array called values, the input field turns into a JComboBox and the default value is assigned based on the initialSelectionValue, in this case its "A".

Let's say I don't want to provide options to the users and expect them to key in the values manually, but I still want to provide an initial value. All I have to do for that is, make the selectionValues parameter as null.


For further reference, have a look at the javadoc for JOptionPane: JOptionPane

Author: Thiagu

Tuesday, July 13, 2010

String Vs. StringBuffer Vs. StringBuilder


String

StringBuffer

StringBuilder

Mutable

No

Yes

Yes

Synchronized

No

Yes

No

Serialized

Yes

Yes

Yes



When to use these classes?

String – When your text is not going to change
StringBuffer – When your text will be changed by multiple threads
StringBuilder – When your text will be changed by a single thread

Comparison between mutable and immutable objects

String is always considered an immutable class because String objects can't be changed. Usually the "+" or concatenation operator is used to add a character sequence to an existing String variable, but that does not mean its making alterations to the existing object. If you have a look at the example below first a String object is created and then it is concatenated with another String which eventually creates a new object for Str1 to refer to in the memory.


String str1 = "abc"; //1st Object

str1 = str1 + "d"; //2nd Object


The scenario is different for a StringBuffer or a StringBuilder. Once an object is created, it can be changed as many times as possible using methods like append(). If you look at the example below, 1st an object is created and later changes are made to the existing object in the memory using append method.

StringBuffer sbf1 = new StringBuffer("abc"); //1st Object


sbf1.append("d"); //appends "d" to the 1st Object




Author: Thiagu

Monday, July 12, 2010

Updating an existing Trans-SQL Job's interval programmatically

The title itself suggests that the topic about to be discussed now has very little thing to do with Java, but this is something that any Java programmer needs to know when they are working along with SQL Server database.

Let’s look at the scenario now, let’s say you have created a Trans-SQL JOB to run a particular process periodically, but you want to provide the feasibility for the users to change the frequency to run the Trans-SQL JOB from your Java application itself. Now how is that possible? Ya, that’s what we are about to explore now, how to change the frequency from the Java application itself.
First create a JDBC connection as shown below
Once you have created the Connection class, make use of it in the main class to establish the connection with the Database. Create a callable statement where you will call the SQL Server System stored procedure called “sp_update_schedule”. Take note that the parameters assigned below are mandatory parameters for the said Stored Procedure. If you look at the code below, the value of the “schedule_id” is assigned to “10” and “name” is assigned to “null”. This is because either 1 value only should be initialized for “sp_update_schedule” to identify the schedule need to be updated.
For further reference about “sp_update_schedule”, please refer to: http://msdn.microsoft.com/en-us/library/ms187354.aspx

Author: Thiagu