I write this blog to share the solutions and problems I have found with fellow software engineers. I also do it to remind myself of what I've already figured out ;)

Thursday, June 28, 2007

Example of javax.swing.Timer

*
* MainFrame.java
*
* Created on June 27, 2007, 9:15 AM
*/

package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;

/**
*
* @author Woody
*/
public class MainFrame extends javax.swing.JFrame implements ActionListener {

static Thread thread1;
static Logger log = Logger.getLogger("MainFrame");
static StringBuffer log2 = new StringBuffer();
static Timer t;

/** Creates new form MainFrame */
public MainFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {

}//


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

thread1.interrupt();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
thread1 = new Thread1();
t = new Timer(100, this);
t.setDelay(100);
t.start();
thread1.start();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {

new MainFrame().setVisible(true);
}
});

}

public javax.swing.JButton getJButton1() {
return jButton1;
}

public void setJButton1(javax.swing.JButton jButton1) {
this.jButton1 = jButton1;
}

public javax.swing.JButton getJButton2() {
return jButton2;
}

public void setJButton2(javax.swing.JButton jButton2) {
this.jButton2 = jButton2;
}

public javax.swing.JButton getJButton3() {
return jButton3;
}

public void setJButton3(javax.swing.JButton jButton3) {
this.jButton3 = jButton3;
}

public javax.swing.JScrollPane getJScrollPane1() {
return jScrollPane1;
}

public void setJScrollPane1(javax.swing.JScrollPane jScrollPane1) {
this.jScrollPane1 = jScrollPane1;
}

public javax.swing.JTextArea getJTextArea1() {
return jTextArea1;
}

public void setJTextArea1(javax.swing.JTextArea jTextArea1) {
this.jTextArea1 = jTextArea1;
}

public void actionPerformed(ActionEvent actionEvent) {
jTextArea1.setText(log2.toString());
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration

}

Friday, June 22, 2007

Using javax.persistence and SQL Server stored procedures

EntityManagerFactory emf = Util.getEMFFactoryCoreRDSce();
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery("EXECUTE dbo.getNationalCEDCount");
Vector i = (Vector) q.getSingleResult();

Sunday, June 17, 2007

Producer Consumer Example using Java.Util.Concurrent and a CompletionService

Blogger screws up indentations, so sorry the source looks ugly. The code does work however.

Main Class

package test;

import java.util.concurrent.*;

public class Main {

public static CompletionService cs;
private final static int poolSize = 4;
public static volatile int counter = 1;
/**
* @param args
*/
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
cs = new ExecutorCompletionService(pool);
(new Producer()).run();
(new Consumer()).run();
}

}

Producer Class
package test;

public class Producer extends Thread {

@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Submitting Task " + i);
Main.cs.submit(new Task1(i));
}
}

}

Consumer Class

package test;

import java.util.concurrent.ExecutionException;

public class Consumer extends Thread {

@Override
public void run() {
int i = 0;
while(true) {
try {
Integer j = Main.cs.take().get();
i++;
System.out.println("Took out task " + i + " with result:" + j);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Task Class

package test;

import java.util.concurrent.Callable;

public class Task1 implements Callable {

int id;

Task1 (int i){
id = i;
}

public Object call() throws Exception {
Thread.sleep(1000*Main.counter);
Main.counter++;
System.out.println("In Task1 and just iterated counter to:" + Main.counter);
return Main.counter;
}

}