At work, I use Linux for building our software package. Most of the time we will give build just before lunch, so that during the lunch break build will be done. And we, the lazy developers wait till the last minute to commit.. so people will be waiting for us to commit, so that they can start the build before lunch.. I got this idea today, so that we need not wait till everybody completes. We just delay the launch of the build by say 15 minutes so that you can execute the command and go for your lunch.. the lazy developer will commit in the meantime and the build kick starts without your presence..
Just 2 lines.. see below:
echo "Waiting for $1 seconds to launch $2"
sleep $1
$2
I saved the script as defer and put it in the /usr/local/bin directory. I did chmod to make the file executable.. so if i need to defer my build for 15 minute == 900 seconds
so my command will be:
defer 900 "make mytool"
there you go.. after 15 minutes your make will start... hurray!!
A realllly simple, handy script...
Monday, January 12, 2009
Paging database query
This is a very common scenario in web application where we have to show data in the DBMS in pages, since we cannot show indefinitely large quantity data in a browser in one go. I never tried it before and was wonder if it is easy to do that. To my surprise it is really easy to page the data in a web application. O/R mapping tools like hibernate makes the job even more easier.
Every RDBMS systems has a SQL syntax for limiting the amount of data chucked out.
With MySQL:
(any query you need to write) LIMIT {OFFSET}, {RECORD_PER_PAGE}
With Hibernate:
Javalobby article on paging
Devx article on J2EE paging
Every RDBMS systems has a SQL syntax for limiting the amount of data chucked out.
With MySQL:
(any query you need to write) LIMIT
With Hibernate:
Query q = session.createQuery("...");Pretty simple isn't it.. Please follow the below links for more reading..
q.setFirstResult(start);
q.setMaxResults(length);
Javalobby article on paging
Devx article on J2EE paging
Friday, July 20, 2007
Using Swing Worker
In any GUI application the most important thing is responsiveness to user action. To keep our application responsive requires careful design and coding. As we know all the swing related event will be processed by only one thread called AWT event dispatcher thread. Its the job of this thread to do painting of components shown on the screen. If you block AWT event thread for quite sometime it will make your application look unresponsive.
In this post, I will share my experience with this and how I overcame it. I will also show you a small code snippet, which you can use it other Swing Application.
To make your application look responsive you should not do heavy weight operation in AWT thread. For example, on click of a button you need to fetch data from a different network, which will take sometime, so never do this in AWT thread. similarly if you add 2 number on click of a button, you can do it in AWT dispatch thread itself.
In the sample application that we are going to design, we will have a text box which will be filled by data (after doing heavy weight operation) . We will have 2 different buttons. One button will do the heavy weight operation in AWT Thread itself and other will use swing worker thread.
The code snippet below shows how it can be done.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingWorkerExample implements ActionListener
{
private JFrame frame;
JTextField field;
// create necessary user interface
public void init()
{
frame = new JFrame("Test for Swing Worker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
field = new JTextField(20);
gc.gridx = gc.gridy = 0;
panel.add(field,gc);
gc.gridy++;
JButton button = new JButton("Do Work in background"); // button that does heavy work in background
button.setActionCommand("Work");
button.addActionListener(this);
panel.add(button,gc);
gc.gridy++;
button = new JButton("Do Work"); // button that does heavy work in awt thread ;(
button.setActionCommand("Work2");
button.addActionListener(this);
panel.add(button,gc);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
// no click of a mouse button, called from AWT thread
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Work"))
{
DelaySwingWorker worker = new DelaySwingWorker(field);
worker.startWork();
}
else
{
try
{
Thread.sleep(10000);
field.setText("Data fetched : " + 25);
}
catch(Exception ex)
{
}
}
}
// out swing worker thread.
abstract public static class SwingWorker
{
public void startWork()
{
Thread t = new Thread(new Runnable() { // create a new thread for heavy operation
public void run()
{
SwingWorker.this.getData(); // let the swing worker get the data
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
SwingWorker.this.setData(); // update the data. This will be called from AWT thread
// so that you can safely do UI related update with the data
}
});
}
});
t.start();
}
abstract public void getData(); // abstract method will be implemented by the actual data fetchers
abstract public void setData();
}
// a sample class which simulates heavy weight operatoin
public static class DelaySwingWorker extends SwingWorker
{
private int data;
private JTextField field;
public DelaySwingWorker(JTextField f)
{
this.field = f; // field on which we will show the updated data.
}
public void getData()
{
try
{
Thread.sleep(10000); // heavy weight operation
data = 20; // data fetched
}
catch(Exception e)
{
}
}
public void setData()
{
field.setText("Data fetched is : " + 20);
}
}
public static void main(String[] args)
{
final SwingWorkerExample ex = new SwingWorkerExample();
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
ex.init();
}
});
}
}
Now run the application, click the first button, now minimize and maximize the application. It should look normal.
now click the second button and minimize and maximize the application, you should see window being grayed out. This is because AWT thread is blocked in a heavy weight operation and hence it cannot dispatch paint event to get the components painted.
In this post, I will share my experience with this and how I overcame it. I will also show you a small code snippet, which you can use it other Swing Application.
To make your application look responsive you should not do heavy weight operation in AWT thread. For example, on click of a button you need to fetch data from a different network, which will take sometime, so never do this in AWT thread. similarly if you add 2 number on click of a button, you can do it in AWT dispatch thread itself.
In the sample application that we are going to design, we will have a text box which will be filled by data (after doing heavy weight operation) . We will have 2 different buttons. One button will do the heavy weight operation in AWT Thread itself and other will use swing worker thread.
The code snippet below shows how it can be done.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingWorkerExample implements ActionListener
{
private JFrame frame;
JTextField field;
// create necessary user interface
public void init()
{
frame = new JFrame("Test for Swing Worker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
field = new JTextField(20);
gc.gridx = gc.gridy = 0;
panel.add(field,gc);
gc.gridy++;
JButton button = new JButton("Do Work in background"); // button that does heavy work in background
button.setActionCommand("Work");
button.addActionListener(this);
panel.add(button,gc);
gc.gridy++;
button = new JButton("Do Work"); // button that does heavy work in awt thread ;(
button.setActionCommand("Work2");
button.addActionListener(this);
panel.add(button,gc);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
// no click of a mouse button, called from AWT thread
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Work"))
{
DelaySwingWorker worker = new DelaySwingWorker(field);
worker.startWork();
}
else
{
try
{
Thread.sleep(10000);
field.setText("Data fetched : " + 25);
}
catch(Exception ex)
{
}
}
}
// out swing worker thread.
abstract public static class SwingWorker
{
public void startWork()
{
Thread t = new Thread(new Runnable() { // create a new thread for heavy operation
public void run()
{
SwingWorker.this.getData(); // let the swing worker get the data
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
SwingWorker.this.setData(); // update the data. This will be called from AWT thread
// so that you can safely do UI related update with the data
}
});
}
});
t.start();
}
abstract public void getData(); // abstract method will be implemented by the actual data fetchers
abstract public void setData();
}
// a sample class which simulates heavy weight operatoin
public static class DelaySwingWorker extends SwingWorker
{
private int data;
private JTextField field;
public DelaySwingWorker(JTextField f)
{
this.field = f; // field on which we will show the updated data.
}
public void getData()
{
try
{
Thread.sleep(10000); // heavy weight operation
data = 20; // data fetched
}
catch(Exception e)
{
}
}
public void setData()
{
field.setText("Data fetched is : " + 20);
}
}
public static void main(String[] args)
{
final SwingWorkerExample ex = new SwingWorkerExample();
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
ex.init();
}
});
}
}
Now run the application, click the first button, now minimize and maximize the application. It should look normal.
now click the second button and minimize and maximize the application, you should see window being grayed out. This is because AWT thread is blocked in a heavy weight operation and hence it cannot dispatch paint event to get the components painted.
Friday, July 13, 2007
Developer's life !
Its been quite a long since I updated my blog, because I don't find enough time (rather don't have a solid subject to write on! may be!) After reaching many developer's blog, who share their knowledge of software development, challenges in software development and solutions, I too have decided to write my experience in the process of software development. Yes I am new to this field where there are many experts, but the information that I may provide is nevertheless.
So here I am to share my experience, to share problems faced, bottle-necks I faced, hours spent on google trying to find the solution. Really don't know how regular I will be in updating my blog, but any how I have decided to give it a try.
To gain enough expertise in handling in a large software project is a big challenge and I have just now entered to this field. New jargons, new terms - but life is never interesting without new things. Apart from developing software as a job (that's what I'm getting paid for!), I would love to do that as a hobby as well.
The stuff that I recently developed can be used for converting java code to html (which does some keyword highlighting). You can use this software to convert java code to html so that it can be posted onto a blog or somewhere. You can download it from here
Currently I am working on software called JInspector. With this software you can inspect jar files, create jar files, assists you in creating MANIFEST files etc.,
My goals for creating this piece of software are:
1. Learn about JTree, creating custom cell renderer
2. Learn to use Zip Streams
3. last, but not the least, java swings.
So here I am to share my experience, to share problems faced, bottle-necks I faced, hours spent on google trying to find the solution. Really don't know how regular I will be in updating my blog, but any how I have decided to give it a try.
To gain enough expertise in handling in a large software project is a big challenge and I have just now entered to this field. New jargons, new terms - but life is never interesting without new things. Apart from developing software as a job (that's what I'm getting paid for!), I would love to do that as a hobby as well.
The stuff that I recently developed can be used for converting java code to html (which does some keyword highlighting). You can use this software to convert java code to html so that it can be posted onto a blog or somewhere. You can download it from here
Currently I am working on software called JInspector. With this software you can inspect jar files, create jar files, assists you in creating MANIFEST files etc.,
My goals for creating this piece of software are:
1. Learn about JTree, creating custom cell renderer
2. Learn to use Zip Streams
3. last, but not the least, java swings.
Wednesday, May 2, 2007
Java <-> HTML
Have Java code, put it some where on a web page, that looks good and nice as it looks in an IDE. So do it all you need is Java to HTML converter. There are tons available on the net, but that didn't stop me from writing my own.
A console based JavaHTMLConvertor is ready. Now just have to decorate it with some GUI to make is user friendly. It's on the incubator and I should be able to bake it tomorrow ;) and it will be available for download.
A console based JavaHTMLConvertor is ready. Now just have to decorate it with some GUI to make is user friendly. It's on the incubator and I should be able to bake it tomorrow ;) and it will be available for download.
Code snippet is available at :
http://javaticks.googlepages.com/threadPool.html
There is a mistake in the above program, which tries to interrupt all the other thread in pool on emergency shutdown. This is not possible. A thread can only interrupt itself and not other threads.
http://javaticks.googlepages.com/threadPool.html
There is a mistake in the above program, which tries to interrupt all the other thread in pool on emergency shutdown. This is not possible. A thread can only interrupt itself and not other threads.
A sample Thread Pool implementation..
Many jobs at a time, but no so many!
We have many thread pool implementations available in Java, but still I would like to create my own, just to understand the concepts and here I am to share that experience with you all..
Before we start coding, let us first think about the requirement of a thread pool.
1. We have a pool of thread (may be on demand or not). The no. of threads in a pool is fixed. say 5 or 10.
2. We add jobs to the pool.
3. If any thread is free in the pool, then it would jump in and take up the job.
4. If there are no free threads in pool (that means all the threads are working), the job will be waiting to be handled by some thread, which becomes available after completing the current job.
That's it. Problem at hand is simple, we just need to code that's it...!!
ZZZzzzz.. (3 hours past)
argh..! code is ready for the post. But I have a big problem. I don't want to just copy and paste my java code, it looks dirty.. So I want to convert my Java to HTML so that I can put it here. So I searched the net and found many. Then I thought, why don't I write my own? It's going to be very nice to do it. So I decided to save the post as a draft, and continue it once Java to HTML tool kit is ready..
Note: For those who think, how do I then posted formatted java code in Previous post, what I usually used to do is, go to http://forums.sun.com and write the code using code format tag, then click preview and then copy and paste it over here! that's it.. But this time that doesn't work (because of a bug in the HTML formatting in sun website).
We have many thread pool implementations available in Java, but still I would like to create my own, just to understand the concepts and here I am to share that experience with you all..
Before we start coding, let us first think about the requirement of a thread pool.
1. We have a pool of thread (may be on demand or not). The no. of threads in a pool is fixed. say 5 or 10.
2. We add jobs to the pool.
3. If any thread is free in the pool, then it would jump in and take up the job.
4. If there are no free threads in pool (that means all the threads are working), the job will be waiting to be handled by some thread, which becomes available after completing the current job.
That's it. Problem at hand is simple, we just need to code that's it...!!
ZZZzzzz.. (3 hours past)
argh..! code is ready for the post. But I have a big problem. I don't want to just copy and paste my java code, it looks dirty.. So I want to convert my Java to HTML so that I can put it here. So I searched the net and found many. Then I thought, why don't I write my own? It's going to be very nice to do it. So I decided to save the post as a draft, and continue it once Java to HTML tool kit is ready..
Note: For those who think, how do I then posted formatted java code in Previous post, what I usually used to do is, go to http://forums.sun.com and write the code using code format tag, then click preview and then copy and paste it over here! that's it.. But this time that doesn't work (because of a bug in the HTML formatting in sun website).
Subscribe to:
Posts (Atom)