|
Hi All, I’m using SwingWorker 3 as an inner class to perform
an HTTP request, and persist the results to a DB. The UI has a start button which creates and starts the
SwingWorker when clicked. The UI also includes a stop button to stop the SwingWorker,
and reset the UI buttons. Unfortunately, the problem I’m experiencing is that
when I call SwingWorker.interrupt, it doesn’t actually stop the
SwingWorker. It still continues to read from the inputstream. Any ideas how I can fix this? Here’s the suspect code: private void
jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {
setSearchMode(false); if (worker !=
null)
worker.interrupt(); } private void jButtonStartActionPerformed(java.awt.event.ActionEvent
evt) {
setSearchMode(true); worker = new
SwingWorker() {
Keywordsearch keyword = null;
public Object construct() {
try{
if (jRadioButton1.isSelected()){
maxTime = txtOption1.getValue();
} else if (jRadioButton2.isSelected()){
maxResults = txtOption2.getValue(); }
else if (jRadioButton3.isSelected()){
maxTime = txtOption3_1.getValue();
maxResults = txtOption3_2.getValue();
}
//A map of TableViewModel.row and Keywordsearch objects
Map<Integer,Keywordsearch> keywordSearchMap = new
HashMap<Integer,Keywordsearch>();
final TableViewModel model = (TableViewModel)fTable.getTable().getModel();
for (int row = 0; row < model.getRowCount(); row++){
Boolean isSelected = (Boolean)model.getValueAt(row, 0);
if (isSelected.booleanValue()){
Keywordsearch keywordSearch = keywordSearchDAO.findById(
model.getCurrentPK(row) );
keywordSearchMap.put(new Integer(row), keywordSearch);
}
}
//run search per keyword
for ( final int row : keywordSearchMap.keySet() ){
keyword = keywordSearchMap.get( new Integer(row) );
final int resultCount = doHttpRequest( getUrl( keyword.getTerm() ) );
//update UI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
model.setValueAt(new Boolean(true), row, 2);
model.setValueAt(String.valueOf(resultCount), row, 3);
}
});
}
} catch (MalformedURLException me) {
fLogger.severe(me.toString());
} catch (IOException ioe) {
fLogger.severe(ioe.toString());
} catch (Throwable t) {
t.printStackTrace(); //
fLogger.severe(t.toString());
}
return null;
}
private int doHttpRequest(URL aHttpRequest) throws IOException {
LineNumberReader htmlReader = null;
Set keywordSearchResults = keyword.getKeywordsearchresults();
if (keywordSearchResults == null){
keywordSearchResults = new HashSet();
}
try {
fLogger.info("Connecting to server...");
InputStream input = aHttpRequest.openStream();
fLogger.info("Connected!");
initTimer();
htmlReader = new LineNumberReader(new InputStreamReader(input));
String line = null;
while ( (line = htmlReader.readLine()) != null ) {
System.out.println("response line: " + line);
Keywordsearchresult keywordResult = parseLine(line);
if (keywordResult != null){
keywordSearchResultDAO.saveOrUpdate(keywordResult);
keywordSearchResults.add(keywordResult);
}
}
}finally {
try {
keyword.setKeywordsearchresults(keywordSearchResults);
if (htmlReader != null) htmlReader.close();
} catch (IOException ex) {
fLogger.warning("Could not close HTTP connection.");
}
}
return keywordSearchResults.size();
}
private Keywordsearchresult parseLine(String line){
Keywordsearchresult ksr = null;
Matcher matcher = pattern.matcher(line);
if(matcher.find()) {
ksr = new Keywordsearchresult();
String hint = matcher.group(Consts.MATCHER_GROUP_MATCH);
if (hint.equals("EXACT_MATCH")){
ksr.setType(Keyword.Type.EXACT_MATCH.ordinal());
}else{
ksr.setType(Keyword.Type.BROAD_MATCH.ordinal());
}
ksr.setValue(matcher.group(Consts.MATCHER_GROUP_VALUE));
ksr.setKeywordsearch(keyword);
KeywordsourceDAO sourceDAO = new KeywordsourceDAO();
Keywordsource source = sourceDAO.findByName("Google"); //HARD CODED
FOR NOW.
ksr.setKeywordsource(source);
}
return ksr;
}
//Runs on the event-dispatching thread.
public void finished() {
setSearchMode(false);
}
private URL getUrl(String searchTerm) throws MalformedURLException {
StringBuffer buf = new StringBuffer(Consts.SEARCH_BROKER_URL);
buf.append("?q="+searchTerm);
if (maxResults > 0)
buf.append("&z="+maxResults);
if (maxTime > 0)
buf.append("&t="+maxTime);
if (timeUnit != null)
buf.append("&u="+timeUnit.toString());
Country country = Util.getUserPreferenceCountry();
if (country != null)
buf.append("&c="+country.getCode());
Language
lang = Util.getUserPreferenceLanguage();
if (lang != null)
buf.append("&l="+lang.getCode()); String url = "">
System.out.println(url);
return new URL( url );
} }; worker.start(); } Enrico Goosen Software Developer SAICOM TECHNOLOGY TEL: +2721
555 0726 FAX: +2721 555 0821 CEL: +2783 305 5676 EMAIL: [EMAIL PROTECTED] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/CTJUG-Forum -~----------~----~----~----~------~----~------~--~--- |
- [CTJUG Forum] SwingWorker 3 Enrico Goosen
- [CTJUG Forum] Re: SwingWorker 3 Enrico Goosen
