Today I stummbled upon something I had not done, this may be old hat for some of you, but none the less I thought it was pretty cool and thought I would share. I was working on a class that needed to kick off a thread, so i started down the path I knew and created a class that implemented Runnable, created the run method, and it looked something like the following:
public class MyThread implements Runnable {
private Boolean running;
public MyThread(Boolean running) {
this.running = running;
}
public void run() {
while (running) {
process();
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
public void process() {
//Do someting cool here
}
public void setRunning(Boolean running) {
this.running = running;
}
}
public class ExecuteThread {
private Boolean running = true;
private MyThread myThread;
public void activate() {
myThread = new MyThread(running);
Thread thread = new Thread(myThread);
thread.start();
}
public void deactivate() {
myThread.setRunning(false);
}
}
Using the ScheduledExecutorService provided in the java.util.concurrent package you can handle the execution of a thread and control the initial delay, delay, and the unit of time with one method call. Something like the following:
public class MyThread implements Runnable {
public MyThread() {
}
public void run() {
process();
}
public void process() {
//Do something cool here
}
}
public class ExecuteThread {
private MyThread myThread;
private final ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
public void activate() {
myThread = new MyThread();
scheduler.scheduleWithFixedDelay
(myThread, 0, 10, TimeUnit.SECONDS);
}
public void deactivate() {
scheduler.shutdown();
}
}
Again, this might be old news for some of you, but I thought this was a very effective way to handle threading and it really cleaned up my code in the end.
Friday, May 18, 2007
Java Threading - ScheduledExecutorService
Posted by Chad Gallemore at 3:22:00 PM 7 comments
Thursday, May 10, 2007
Creating a Binding Component Deployment Plug-In for Netbeans
Recently I have had the pleasure of working on a team developing and open sourcing to java.net some binding components for JBI. One of the tasks we had to complete was integrating our binding components into netbeans. One of the things netbeans provides is a WSDL wizard, so when you are creating new modules the wizard lets you select the type of binding you will be using, and based off that binding will give you a base template of your wsdl with some default values of attributes you need to provide. When I first started I had no idea on how to create a plug in for netbeans. So, this will cover how to create a Binding Component Deployment Plugin. The first thing you will need to get is the
OpenESB installer for netbeans.
- Select File --> New Project
- Under Categories select --> JBI Components
- Under Projects select --> Binding Component Deployment Plugin and click next
- Name your plugin (i.e. NewBCPlugin)
- define your source package (i.e. com.company.netbeans.modules.wsdlextensions.newbc)
- Define your component Name (i.e. newbc).
- Edit your WSDL Extension
- Define your Namespace (i.e. http://schemas.company.com/jbi/wsdl-extensions/newbc/)
- Define your prefix (i.e. newbc)
- And click Finish
Once that is finished you will have a Source Packages directory, and under there you will have two packages something like the following:
- com.company.netbeans.modules.wsdlextensions.newbc
- Bundle.properties
- layer.xml
- newbcWsdlExt.xsd
- com.company.netbeans.modules.wsdlextensions.newbc.resources
You will need to edit the layer.xml file. You will need to update the last section to be something like this.
That should be a good starting point for you on that package. Next we will neeed to create a new package called com.company.netbeans.modules.wsdlextensions.newbc.template,and create the following files under that package:
- Bundle.properties
NewBCTemplateProvider.java
template.xml
NewBCTransport=NewBC Transport
NewBCMessage=NewBC Message
http_//schemas.company.com/jbi/wsdl-extensions/newbc/=NewBC
TEMPLATEGROUP_name=NewBC
TEMPLATEGROUP_prefix_newbc=newbc
TEMPLATE_name_NewBCTransport=NewBC Transport
Next we are going to edit the NewBCTemplateProvider.java, this class is pretty straight forward and should look like the following:
package com.company.netbeans.modules.wsdlextensions.newbc.template;
import java.io.InputStream;
import org.netbeans.modules.xml.wsdl.ui.
spi.ExtensibilityElementTemplateProvider;
import org.openide.util.NbBundle;
public class NewBCTemplateProvider
extends ExtensibilityElementTemplateProvider {
static final String templateUrl =
"/com/company/netbeans/modules/wsdlextensions/newbc
/template/template.xml";
public InputStream getTemplateInputStream() {
return NewBCTemplateProvider.class
.getResourceAsStream(templateUrl);
}
public String
getLocalizedMessage(String str, Object[] objects) {
return NbBundle.getMessage
(NewBCTemplateProvider.class, str, objects);
}
}
For this code to compile you will have to update your libraries by doing the following:
- Right click on your project --> Properties
- Select Libraries
- Add the following libraries
- WSDL UI (You will need to edit this library and select use Implementation Version)
- Utilities API
- WSDL UI (You will need to edit this library and select use Implementation Version)
Next we will look at creating the template.xml; this is the file where you add any attribute that is defined in your NewBCExt.xsd to include default vaules. Once this plugin
is installed these default values show up as properties in the WSDL Editor. Your template.xml should look something like this.
Finally you well need to add META-INF/services directory which should be located under your src directory (newbc/src/META-INF/services). This should contain a file called
org.netbeans.modules.xml.wsdl.ui.spi.
ExtensibilityElementTemplateProvider
and it should say the following:
com.gestalt.netbeans.modules.wsdlextensions.newbc.
template.NewBCTemplateProvider
Now you are at a point you can test the plugin. You will need to right click on the project and perform a clean and build. Next you will need to test, right click the project and select "Install in Target Platform". This will start a new instance of Netbeans with your plugin installed. Once it is started create a new project and under Service Oriented Archietecture select BPEL Module. Once the project is created right click on Process Files and select create new WSDL Document. Click next through the Wizard until you get to selecting the type of binding. Here you will want to select the drop down box called Binding Type and select your new plugin (If it isn't showing up in the list there is something missing).
Posted by Chad Gallemore at 9:28:00 PM 1 comments
Labels: Binding Component Plugin, JBI, Netbeans