Arquivo

Posts Tagged ‘Tutorial’

Process check script for Linux

Sometimes I need to keep checking the some process is running in my servers. So, I created the following script and add it to Crontab. If I have problems, Ill receive an email.
This can be useful to someone!
In my example, Im searching for a process called zookeeper.

#!/bin/bash
SERVER="PROD 134"
ZK=`ps aux | grep zookeeper | grep -v "grep" | wc -l`

if [ $ZK -ge 1 ]
   then
        echo "ZK is Running"
   else
        echo "ZK is not Running" | mail -s "Zookeeper is not running - $SERVER" myemail@br.ibm.com
fi
Categorias:Linux Tags:, , , ,

Creating a collection in IBM Watson Explorer crawling from Database

Perform a database Craw from a collection its something very common. With IBM Watson Explorer this is something very easy to do. In my example, Ill create a collection and will perform a simple query in a IBM DB2 database, but, the steps will be very similar for other databases, you just need to keep in mind that you will need the correct driver.

1- Put the driver in place:

Get the database jdbc and put in the correct folder, usually it is something like /opt/IBM/dataexplorer/WEX-11_0_2/Engine/lib/java/database/.

2- Create the collection copying defaults from default:

Selection_188.jpg

3- Add a new seed, this is where your collection will get data:

Selection_189.jpg

4- Choose Database:

Selection_190.jpg

5- Enter your database settings and the query that will be performed:

Selection_191.jpg

6- Its done, now you can test:

Selection_192.jpg

7- This can take a while depending on your query and connection, but when it finish, it will show some rows that the query returned in the following format. To see some row data, click Crawler XML:

Selection_193.jpg

8- Here is your data:

Selection_194

9- Now that we see that its working, you can start your craw. This step will feed your collection and can take a good time depending on your amount of data:

Selection_195

10-You must see Craw activity:

Selection_196

11- You can query your collection now to test, just enter your term and click search in the left options:

Selection_197

12- You will see something like this:

Selection_198

Thats it, you have created a collection that get data from Database!

Pequeno exemplo de Threads em Java

Compartilhando uma pequena solução utilizada em POCs (provas de conceito) quando preciso demonstrar alguma coisa utilizando Threads, segue um pequeno trecho que pode ser útil para alguém, e certamente para mim mesmo (quem escreve e compartilha – nunca esquece… ou quase isso).

Criei uma classe para ser minha gerenciadora de thread:

package br.com.ibm.threads;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class ThreadExecutorMaganer {
private ExecutorService executor;
private long timeout;

private List<Callable<String>> callables;

public ThreadExecutorMaganer(int maxThreads, long timeoutInSeconds) {
this.executor = Executors.newFixedThreadPool(maxThreads);
this.callables = new ArrayList<Callable<String>>();
this.timeout = timeoutInSeconds;
}

public void add(Callable<String> callable) {
this.callables.add(callable);
}

public List<Future<String>> start() {
List<Future<String>> futures = null;

try {
futures = executor.invokeAll(callables, timeout, TimeUnit.SECONDS);

executor.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();

executor.shutdownNow();
}

return futures;
}

}

Esta é minha Thread em si (veja que tem um IF la com um sleep só pra provocar erro e testar), ela é do tipo Callable;

package br.com.ibm.threads;

import java.util.concurrent.Callable;

public class CallableTask implements Callable<String>{

private final String tarefa;

public CallableTask(String tarefa) {
this.tarefa = tarefa;
}

@Override
public String call() throws Exception {
System.out.println("Inside call-->" + tarefa);
if (tarefa.equals("C")){
System.out.println("Sleeping 6 seconds");
Thread.sleep(6000);
}
return tarefa;
}

}

E esta é minha classe principal que invoca o circo:

package br.com.ibm.threads;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import com.ibm.services.tools.wexws.utils.ThreadExecutorMaganer;

public class CallThreadExecutorMaganer {

public static void main(String[] args) {
String[] restfulUrls = "A,B,C,D,E".split(",");
ThreadExecutorMaganer tem = new ThreadExecutorMaganer(100, 5);

for (String url : restfulUrls) {
tem.add(new CallableTask(url));
}
List<Future<String>> futureResponses = tem.start();

for (Future<String> futureResponse : futureResponses) {
try {
String resp = futureResponse.get();
System.out.println(resp);

}catch (ExecutionException ex){
System.out.println("ExecutionException while getting WEX response="+ex.getCause().getMessage());
}catch (Exception e) {
System.out.println("Fail to query WEX server:"+e.getMessage());
}
}

}

}

Antes que a patrulha critique: É um SIMPLES exemplo, não deve ser utilizado profissionalmente sem uma análise e adequação para seu caso, como tipagens adequadas, tratamento de erros, etc…

Enjoy!

Categorias:JAVA Tags:, , , ,

Reading XML with Java – Quick and simple example

I always need some code to read XML with Java. This is a place holder to me, but, maybe can be useful to other people.

Here is my XML example:

<operator logic="and">
<operator logic="or">
<term field="query" input-type="user" processing="strict" str="は" />
<term field="query" input-type="user" phrase="phrase" processing="strict" str="銀行業務" weight="1" />
<term field="query" input-type="user" processing="strict" str="持つ" />
<term field="query" input-type="user" phrase="phrase" processing="strict" str="java開発者" weight="1.69" />
<term field="query" input-type="user" processing="strict" str="探して" />
</operator>
</operator>

Here is my Java code:

import org.w3c.dom.*;
import org.xml.sax.InputSource;

import javax.xml.parsers.*;
import java.io.*;

public class ParseXML {

	public static void main(String[] args) {
		String xml = "<operator logic=\"or\"><term field=\"query\" input-type=\"user\" processing=\"strict\" str=\"は\" /><term field=\"query\" input-type=\"user\" phrase=\"phrase\" processing=\"strict\" str=\"銀行業務\" weight=\"1\" /><term field=\"query\" input-type=\"user\" processing=\"strict\" str=\"持つ\" /><term field=\"query\" input-type=\"user\" phrase=\"phrase\" processing=\"strict\" str=\"java開発者\" weight=\"1.69\" /><term field=\"query\" input-type=\"user\" processing=\"strict\" str=\"探して\" /></operator>";
		try {	
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
			doc.getDocumentElement().normalize();
			
			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			NodeList nList = doc.getElementsByTagName("term");
			System.out.println("----------------------------");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				System.out.println("\nCurrent Element :" + nNode.getNodeName());
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					System.out.println("processing : " + eElement.getAttribute("processing"));
					System.out.println("str : " + eElement.getAttribute("str"));
	            }
	         }
	      } catch (Exception e) {
	         e.printStackTrace();
	      }
	}
}
Categorias:JAVA Tags:, , , , , , ,

Introdução a Big Data e Apache Solr

Para quem está interessado em Big Data e além disso quer algo prático utilizando Apache Solr, disponibilizo um conjunto de slides que podem ser utilizados por Estudantes, Professores e profissionais. Usem e distribuam a vontade!

Criando um Web Service Restful com Jersey

Criei um conjunto de slides bem simples e objetivo com o passo a passo para se criar um Web Service RESTful utilizando a API Jersey. Para quem quer conhecer esse mundo, acredito que vá ajudar bastante.

Tenha em mente que implementar um Web Service é relativamente simples, porém, preocupe-se sempre com a segurança e volumetria (carga, stress, usuários, etc), pois isso tende a derrubar muito servidor por ai!

Enjoy!

Criando um CRUD com Ionic e SQLite para IPhone e Android)

No conjunto de slides abaixo mostro um passo a passo de como criar uma aplicação móvel hibrida, que deverá rodar em Android e IPhone, utilizando o framework Ionic (Cordova, Angular, Bootstrap) e acessando um banco SQLite, e efetuará as operações de CRUD (Create, Retrieve, Update e Delete).

O código fonte encontra-se no meu GitHub.

Enjoy!

Passo a passo para a criação de uma aplicação híbrida para IPhone e Android

Criei uma apresentação com o passo a passo para a criação de uma aplicação híbrida para IPhone e Android, utilizando o framework Ionic (Angular + Cordova + Bootstrap).

Nela, iremos criar uma simples aplicação que se conecta em um feed Json do WordPress e lista os posts deste blog. Iremos testar no navegador e no Android. Eis a apresentação.

Para quem quer ver um pouco mais sobre os conceitos antes de partir para esta implementação, veja meu post anterior, que contém esta apresentação:

Apresentação sobre Desenvolvimento Móvel Híbrido

A apresentação a seguir mostra alguns conceitos sobre desenvolvimento móvel hibrido e exemplos de AngulaJS e um projeto Ionic. Vale a pena para quem quer entrar para este mundo. Enjoy!

FindBugs x Checkstyle x PMD

Featured imageInúmeras pessoas fazem a comparação entre as ferramentas FindBugs, Checkstyle e PMD. Vale dizer que as três permitem a escrita de um melhor código fonte, o inspecionando em busca de possíveis problemas e gerando relatórios para que o desenvolvedor possa tomar providências.

Porém, a abordagem delas é um pouco distinta:

  • FindBugs: Funciona procurando padrões de Bugs. Analise o Bytecode, e não o código fonte em si. Funciona como se tivesse um banco de dados interno com os bugs comuns, e vasculha seu código procurando por esses bugs. Eventualmente pode dar falsos positivos, apontando erros que no contexto de sua aplicação, não são erros. A própria documentação oficial do Findbugs diz: “In practice, the rate of false warnings reported by it is less than 50%”.
    http://findbugs.sourceforge.net/index.html
  • CheckStyle: Seu foco é em problemas relacionados as convenções e padrões Java, por exemplo: ausência de Java docs, abertura de chaves { depois de Ifs, etc. Ele varre o código fonte de seu projeto.
    http://checkstyle.sourceforge.net/
  • PMD: Vasculha o código fonte procurando por más práticas e problemas de código por exemplo: import com *, muitos métodos em classes, etc.
    http://pmd.sourceforge.net/

Em geral o PMD e o Checkstyle são mais similares. Todavia, eu recomendo o uso das 3 ferramentas em seu projeto, e além disso, que sejam criados arquivos de configurações para CADA ferramente e que os mesmos sejam utilizados por TODO o time, garantindo assim, a homogeneidade na produção do seu código fonte.

O uso principalmente do checkstyle e do PMD garantem um código fonte mais legível, de melhor manutenabilidade, tornando-se o trabalho com os mesmos mais eficiente e rápido.

Eu fiz video aulas introdutórias sobre as 3 ferramentas, que ensinam como instalar, configurar e utilizar, como sempre, de maneira simples e objetiva. Seguem os links:

  1. FindBugs
  2. Checkstyle
  3. PMD

Enjoy!