7.5.13

A very simple example to use JDOM along with TagSoup

Just Read and Build the Document

SAXBuilder builder = new org.jdom.input.SAXBuilder("org.ccil.cowan.tagsoup.Parser");
Reader in = new StringReader(pageContent);
org.jdom.Document doc = builder.build(in);
System.out.println(new XMLOutputter().outputString(doc));

Applying with XPath

SAXBuilder builder = new org.jdom.input.SAXBuilder("org.ccil.cowan.tagsoup.Parser");
Reader in = new StringReader(pageContent);
org.jdom.Document doc = builder.build(in);
//System.out.println(new XMLOutputter().outputString(doc));
XPath xpath = XPath.newInstance("//xhtml:div[@class='views-row views-row-3 views-row-odd']");
xpath.addNamespace("xhtml", "http://www.w3.org/1999/xhtml");
List<Element> nodes = xpath.selectNodes(doc);
for(Element el : nodes) {
System.out.println(new XMLOutputter().outputString(el));
}

Applying XPath with Sub-Element

I am not sure why, but it seems that JDom has problems to handle Sub-elements. You have to build a new document to apply with the XPath.

SAXBuilder builder = new org.jdom.input.SAXBuilder("org.ccil.cowan.tagsoup.Parser");
Reader in = new StringReader(pageContent);
org.jdom.Document doc = builder.build(in);
//System.out.println(new XMLOutputter().outputString(doc));
XPath xpath = XPath.newInstance("//xhtml:div[@class='node node-teaser node-article']");
xpath.addNamespace("xhtml", "http://www.w3.org/1999/xhtml");
List<Element> nodes = xpath.selectNodes(doc);
XPath xpathArticle = XPath.newInstance("//xhtml:a[@class='node-title']");
xpathArticle.addNamespace("xhtml", "http://www.w3.org/1999/xhtml");

XMLOutputter xmlOutputter = new XMLOutputter();
for(Element el : nodes) {
String elXml = xmlOutputter.outputString(el);
builder = new org.jdom.input.SAXBuilder();
in = new StringReader(elXml);
org.jdom.Document doc2 = builder.build(in);
Element result = (Element)xpathArticle.selectSingleNode(doc2);
System.out.println(xmlOutputter.outputString(result));
}

How to find out the implementation of Spring Integration's XML Namespace?

When I was reading the documents or books about Spring Integration, I always wanted to find out the underlying implementation, which is the easiest way for me to understand how it works and how it doesn't.

For example,


<int:channel id="queueChannel">
    <queue capacity="25"/>
</int:channel>



<int:poller fixed-rate="5000"/>


What is the underlying support Java class?

First of all, I need to know how to define namespace in Spring.


From this article, I learnt that there are two properties files that are very important to me. And one of them is the entry for me to locate the class I want to know:

* spring.handlers

For Spring Integration (core), this is the content of the file:

http\://www.springframework.org/schema/integration=org.springframework.integration.config.xml.IntegrationNamespaceHandler


In turn, I found out this:

registerBeanDefinitionParser("channel", new PointToPointChannelParser());
registerBeanDefinitionParser("poller", new PollerParser());






A Simple Sample Program for Spring's TaskExecutor

references:




@Test
public void test() throws Exception {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);

executor.initialize();

Runnable task = new Runnable() {

@Override
public void run() {
System.out.printf("Start thread : %d\n", Thread.currentThread().getId());
try {
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
} catch (InterruptedException ex) {
//..
}

System.out.printf("End thread id = %d\n", Thread.currentThread().getId());
}
};

for(int k=0; k<100; ++k)
{
try {
executor.execute(task);
} catch (TaskRejectedException ex) {
System.out.println("Thread rejected. Wait...");
TimeUnit.SECONDS.sleep(1);
}
}
}

1.5.13

How do you find what process is holding a file open in Windows?

ntfs - How do you find what process is holding a file open in Windows? - Server Fault


I've had success with Sysinternals Process Explorer. With this, you can search to find what process(es) have a file open, and you can use it to close the handle(s) if you want. Of course, it is safer to close the whole process. Exercise caution and judgement.
To find a specific file, use the menu option "Find->Find Handle or DLL..." Type in part of the path to the file. The list of processes will appear below.
share|improve this answer

26.4.13

Packages to install for CM10 Compilation in Ubuntu

*  oracle-java6-installer
* schedtool
* xsltproc
* lzop
* flex
* zip
* g++-multilib lib32z1-dev lib32ncurses5-dev lib32readline-gplv2-dev gcc-4.7-multilib g++-4.5-multilib
*  python-software-properties
* git
* build-essential
* bison
* gperf
* unzip
* libxml2-utils

30.3.13

My lottery program


ArrayList<Integer> list = new ArrayList<>();

for(int k=0; k<59; ++k)
list.add(k + 1);

Random r = new Random();

int m = r.nextInt(10000);

while(--m > 0)
r.nextInt(10000);

for(int k=0; k<5; ++k)
{
m = r.nextInt(10000);
while(--m > 0)
r.nextInt(10000);

int xv = r.nextInt(10000) % list.size();
int v = list.remove(xv);
System.out.println(v);
}

System.out.println("");

m = r.nextInt(10000);

while(--m > 0)
r.nextInt(10000);

System.out.println(r.nextInt(10000) % 35 + 1);

29.3.13

A simple program to test two different channels of JMS

package demo;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ActiveMQWaitingListTest {
 
 private ConnectionFactory connFactory;
 private Connection conn;
 private BrokerService broker;

 @Before
 public void setUp() throws Exception {
  broker = BrokerFactory.createBroker("broker:tcp://0.0.0.0:61618");
  broker.start();
  
  connFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61618");
  conn = connFactory.createConnection();
  conn.start();   
 }
 
 @After
 public void tearDown() throws Exception {
  conn.close();
  broker.stop();
 }

 @Test
 public void test() throws Exception {
  final Session s1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
  final Session s2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
  final Queue queue = s1.createQueue("Q");
  final Topic topic = s1.createTopic("T");
  final Queue reply = s1.createQueue("R");
  
  final MessageProducer p1 = s1.createProducer(queue);
  final MessageProducer p2 = s2.createProducer(topic);
  
  final AtomicBoolean flag = new AtomicBoolean();
  
  ExecutorService service = Executors.newCachedThreadPool();
  
  service.execute(new Runnable() {
   
   @Override
   public void run() {
    try {
     doRun();
    } catch(Exception ex) {
     ex.printStackTrace();
    }
   }
   
   public void doRun() throws Exception {
    //wait for 10 minutes to begin
    
    TimeUnit.MINUTES.sleep(5);
    
    ConnectionFactory f2 = new ActiveMQConnectionFactory("tcp://10.0.0.17:61618");
    
    //block the outer conn variable
    Connection conn = f2.createConnection();
    conn.start();
    
    Session sx1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sx2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    
    MessageConsumer cx1 = sx1.createConsumer(queue);
    final AtomicInteger counter = new AtomicInteger();
    
    cx1.setMessageListener(new MessageListener() {
     
     @Override
     public void onMessage(Message message) {
      //delay for 10 millisecond
      try {
       counter.incrementAndGet();
       TimeUnit.MILLISECONDS.sleep(10);
      } catch (Exception ex) {
       ex.printStackTrace();
      }
     }
    });
    
    MessageConsumer cx2 = sx2.createConsumer(topic);
    TextMessage msg = (TextMessage)cx2.receive();
    
    MessageProducer p = sx2.createProducer(reply);
    p.send(sx2.createTextMessage(msg.getText() + " " + counter.get()));
    
    conn.close();
   }
  });
  
  service.execute(new Runnable() {
   
   @Override
   public void run() {
    try {
     doRun();
    } catch(Exception ex) {
     ex.printStackTrace();
    }
    
   }

   private void doRun() throws InterruptedException,
     JMSException {
    while(!flag.get()) {
     TimeUnit.MILLISECONDS.sleep(1);
     p1.send(s1.createObjectMessage(new byte[1024]));
    }
   }
  });
  
  service.execute(new Runnable() {
   
   @Override
   public void run() {
    try {
     doRun();
    } catch(Exception ex) {
     ex.printStackTrace();
    }
    
   }

   private void doRun() throws InterruptedException,
     JMSException {
    while(!flag.get()) {
     TimeUnit.MILLISECONDS.sleep(10);
     p2.send(s1.createTextMessage(String.valueOf(System.currentTimeMillis())));
    }
   }
  });
  
  service.shutdown();
  
  Session s3 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
  MessageConsumer c = s3.createConsumer(reply);
  TextMessage msg = (TextMessage)c.receive();
  String text = msg.getText();
  String[] sep = text.split(" ");
  long value = Long.parseLong(sep[0]);  
  int recv = Integer.parseInt(sep[1]);
  
  System.out.printf("It took %d milliseconds to send and receive the message in another topic, while %d msg recv for queue\n", 
    (System.currentTimeMillis() - value), recv);
  
  flag.set(true);
  service.awaitTermination(1, TimeUnit.HOURS);
  
 }

}
And here is the test result: Here is the test result: It took 36 milliseconds to send and receive the message in another topic, while 4 msg recv for queue