find {DIR_PATH} -name 'FILE_NAME_WILDCARD' | xargs wc -l
mercredi 19 février 2014
How to count all the lines of code in a directory recursively
jeudi 13 février 2014
Handling a RuntimeException in a Runnable (Thread)
package edu.test;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis());
throw new RuntimeException("exception");
} catch (RuntimeException e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
}
}, 0, 1000, TimeUnit.MILLISECONDS).get();
System.out.println("exit");
executor.shutdown();
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis());
throw new RuntimeException("exception");
} catch (RuntimeException e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
}
}, 0, 1000, TimeUnit.MILLISECONDS).get();
System.out.println("exit");
executor.shutdown();
}
}
vendredi 3 mai 2013
Submit a package in nexus with shell command line
curl --request PUT --user username:password nexusUrl/packageName --data-binary @packageName
vendredi 1 juin 2012
ActiveMQ Network broker
To Start ActiveMq in network broker mode:
1- Configuration
$ cd $ACTIVEMQ_HOME/
$vi conf/activemq-dynamic-network-broker1.xml
--> add balise to this config file
$ cp conf/jetty.xml conf/jetty2.xml
$ vi conf/jetty.xml conf/jetty2.xml : update port number of jetty web server
$vi conf/activemq-dynamic-network-broker2.xml
--> add
2 - Developpement (producer and consumer)
- Create consumer:
package com.test.jms;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsNetworkBrokerConsumer {
private static ActiveMQConnectionFactory connectionFactory;
private static Connection connection;
private static Session session;
private static Destination destination;
private static boolean transacted = false;
private static String brokerUrl = "failover:(tcp://localhost:61616,tcp://localhost:61618)";
public static void main(String ...strings) throws Exception{
connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("common.queue");
MessageConsumer consumer = session.createConsumer(destination);
connection.setExceptionListener(new ExceptionListener() {
public void onException(JMSException exception) {
System.err.println("Error:" + exception);
}
});
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
System.out.println("New Message : " + message);
}
});
}
}
- And now Create producer
package com.test.jms;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsNetworkBrokerProducer { public static void main(String[] args) throws Exception
{
// Create the connection
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(null, null, "failover:(tcp://localhost:61616,tcp://localhost:61618)");
Connection connection = connectionFactory.createConnection();
connection.start();
// Create the Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// And the Destination queue
Destination destination = session.createQueue("common.queue");
// Create the producer
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
Message m = session.createTextMessage("hello");
//Send 3 messages at a time
//for (int i = 0; i < 3; i++)
// producer.send(m);
producer.send(m);
//shutdown with NO Error handling
connection.close();
}
}
4 - Start consumer : java JmsNetworkBrokerConsumer
5 - Start producer : java JmsNetworkBrokerProducer
1- Configuration
$ cd $ACTIVEMQ_HOME/
$vi conf/activemq-dynamic-network-broker1.xml
--> add
$ cp conf/jetty.xml conf/jetty2.xml
$ vi conf/jetty.xml conf/jetty2.xml : update port number of jetty web server
$vi conf/activemq-dynamic-network-broker2.xml
--> add
2 - Developpement (producer and consumer)
- Create consumer:
package com.test.jms;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsNetworkBrokerConsumer {
private static ActiveMQConnectionFactory connectionFactory;
private static Connection connection;
private static Session session;
private static Destination destination;
private static boolean transacted = false;
private static String brokerUrl = "failover:(tcp://localhost:61616,tcp://localhost:61618)";
public static void main(String ...strings) throws Exception{
connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("common.queue");
MessageConsumer consumer = session.createConsumer(destination);
connection.setExceptionListener(new ExceptionListener() {
public void onException(JMSException exception) {
System.err.println("Error:" + exception);
}
});
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
System.out.println("New Message : " + message);
}
});
}
}
- And now Create producer
package com.test.jms;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsNetworkBrokerProducer { public static void main(String[] args) throws Exception
{
// Create the connection
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(null, null, "failover:(tcp://localhost:61616,tcp://localhost:61618)");
Connection connection = connectionFactory.createConnection();
connection.start();
// Create the Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// And the Destination queue
Destination destination = session.createQueue("common.queue");
// Create the producer
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
Message m = session.createTextMessage("hello");
//Send 3 messages at a time
//for (int i = 0; i < 3; i++)
// producer.send(m);
producer.send(m);
//shutdown with NO Error handling
connection.close();
}
}
3- Start Servers
--> Start Server 1:
- $ bin/activemq console xbean:conf/activemq-dynamic-network-broker1.xml
--> port activeMQ 61616, port jetty 8161
--> Start Server 2 :
- $ bin/activemq console xbean:conf/activemq-dynamic-network-broker2.xml
--> port activeMQ 61618, port jetty 8162
- $ bin/activemq console xbean:conf/activemq-dynamic-network-broker1.xml
--> port activeMQ 61616, port jetty 8161
--> Start Server 2 :
- $ bin/activemq console xbean:conf/activemq-dynamic-network-broker2.xml
--> port activeMQ 61618, port jetty 8162
4 - Start consumer : java JmsNetworkBrokerConsumer
5 - Start producer : java JmsNetworkBrokerProducer
mercredi 23 mai 2012
empty linux cache script
empty-linux-cache.sh
#!/bin/bash ################################################### ### Script de purge du cache de la mémoire RAM ### ### Auteur: Jamel ESSOUSSI ### ### email: jamel.essoussi@gmail.com ### ################################################### echo "Vidage du cache de la mémoire RAM" echo "1" > /proc/sys/vm/drop_caches sleep 5 echo "0" > /proc/sys/vm/drop_caches echo "Vidage du cache terminé ;-)"bash> chmod +x empty-linux-cache.sh
bash> sudo sh empty-linux-cache.sh
mercredi 8 février 2012
Lucene very good search engine
I- Hello Lucene Example:
II- Sample Example (indexing):
package com.ictelecom.lucene.search.test;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import java.io.IOException;
public class HelloLucene
{
public static void main(String[] args) throws IOException, ParseException
{
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
// 1. create the index
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "lucene test");
w.close();
// 2. query
String querystr = args.length > 0 ? args[0] : "lucene test";
// the "title" arg specifies the default field to use
// when no field is explicitly specified in the query.
Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(querystr);
// 3. search
int hitsPerPage = 20;
IndexSearcher searcher = new IndexSearcher(index, true);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i
{
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}
// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}
private static void addDoc(IndexWriter w, String value) throws IOException
{
Document doc = new Document();
doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
II- Sample Example (indexing):
This tutorial sketches a small Application. We want to index text Files and then search the files for words. This tutorial will give you an overview and is not intended to be a working application. The code given is not complete.
There are three classes:
- One for indexing the files
- One for converting File objects to indexable documents
- One for searching the index
public class FileIndexer {public static void index( File file ) { // 1. Convert indexed object to a document // 2. Write document to IndexWriter// directory, where to store the index // files String indexFile = "/tmp/fileindex";// to index documents, they are 'written' or added to // an IndexWriter IndexWriter writer = null; try { File f; boolean create = true; // create index if the directory does not exist if ((f = new File(indexFile)).exists() && f.isDirectory()) { create = false; } else { create = true; } writer = new IndexWriter(indexFile, new Analyzer(), create);writer.mergeFactor = 20; // now add this document to the Index // we use an adapter class, which is given a // file and returns a document, which lucene // can index writer.addDocument(FileDocument.Document(file)); writer.optimize(); } catch(InterruptedException e) { throw new IndexException("Unable to index document.", e); } catch(IOException e) { throw new IndexException("Unable to index document.", e); } finally { close(writer); } }// close writer public static void close(IndexWriter writer) { if(null != writer) { try { writer.close(); } catch(Exception e) { } } } }
The convertion class used in FileIndexer is easy. It converts the File object to a Lucene document. Usually you use a Class with a static method Document as a factory method.
public class FileDocument { // Lucene can only index objects of type // Document. So the to be indexed object // must be converted to this document, // usually with a static methodpublic static Document Document(File file) { // load content of file, get name, path and date // and store them into content, name, date ...Document doc = new Document();DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.GERMANY);// we want to index the fields content, name and date // we can then search for "house" everywhere or // "content:house" in content. // fields can be added that are not indexed but // can be acessed at search time. This is e.g. useful // for storing object primary keys etc. doc.add(Field.Text("content", content)); doc.add(Field.Text("path", path); doc.add(Field.Text("name", name); doc.add(Field.Text("date", df.format(message.getSentDate()))); return doc; } }
The FileSearcher class uses the index to find documents.
- Get a QueryString
- Get an IndexSearcher
- Parse the Query
- Give the parsed Query to the IndexSearcher
- The IndexSearcher will return an Array with matching documents, sorted by hit quality
// Get the query String e.g. from the comand line ...// Create a searcher from the index file String indexFile = "/tmp/fileindex"; Searcher searcher = null; try { searcher = new IndexSearcher(indexFile); } catch(IOException e) { System.out.println("Unable to open index file: " + indexFile); System.exit(1); }// parse the query String. Query query = null; try { // If no prefix for a word is given, then search in // content by default query = QueryParser.parse(queryString, "content", new Analyzer()); } catch(ParseException e) { close(searcher); System.out.println("Unable to parse: " + queryString); System.exit(1); }// get the hits from the searcher for // the given query Hits hits = null; try { hits = searcher.search(query); } catch(IOException e) { close(searcher); System.out.println("IO Error."); e.printStackTrace(); System.exit(1); }// iterate over the results // the results are an array of document try { // display the first 10 results int start = 0; final int HITS_PER_PAGE = 10; int end = Math.min(HITS_PER_PAGE, hits.length());if(hits.length() > 0) { PrintfFormat pf = new PrintfFormat("%-20s %-30s %-30s"); System.out.println(pf.sprintf(new Object[]{"Date","Subject","To"})); for(int i = start; i < end; i++) { System.out.println(pf.sprintf( new Object[] { // retrieve the indexed Fields from the result documents. // we could also get a persisten object key and load // the objects for further attributes to display hits.doc(i).get("path"), hits.doc(i).get("name"), hits.doc(i).get("date")})); } } else { System.out.println("No matching files found."); }
Inscription à :
Articles (Atom)