UTILIZEI O DECOMPILER PARA ACESSAR A CLASSE
package br.com.caelum;
import java.io.PrintStream;
public class TesteMensageria { public TesteMensageria() {}
public static void main(String[] args) { if (args.length == 0) {
System.out.println("use \"envia\" ou \"consome\" como parametro");
return;
}
ConectorJms conectorJms = new ConectorJms();
switch (args[0]) {
case "envia":
int max = parseMax(args);
conectorJms.enviaMensagens(max);
break;
case "consome":
conectorJms.consomeMensagens();
}
conectorJms.close();
}
private static int parseMax(String[] args)
{
if ((args.length < 2) || (args[1] == null)) {
return 1;
}
return Integer.parseInt(args[1]);
}
}
package br.com.caelum;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ConectorJms
{
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
private final InitialContext context;
private final Connection conexao;
private final Destination fila;
public ConectorJms() {
try {
context = createInitialContext();
ConnectionFactory cf = (ConnectionFactory)context.lookup("ConnectionFactory");
conexao = cf.createConnection();
fila = ((Destination)context.lookup("financeiro"));
} catch (NamingException|JMSException e) {
throw new RuntimeException(e);
}
}
public void enviaMensagens(int max)
{
try
{
Session sessao = conexao.createSession(false, 1);
MessageProducer producer = sessao.createProducer(fila);
System.out.println("Enviando " + max + " menagem(ns)");
for (int i = 0; i < max; i++) {
Message msg = sessao.createTextMessage("Mensagem " + (i + 1) + " (" + geraHora() + ")");
producer.send(msg);
}
sessao.close();
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
private String geraHora() {
return SDF.format(new Date());
}
public void close() {
try {
conexao.close();
context.close();
} catch (JMSException|NamingException e) {
throw new RuntimeException(e);
}
}
private InitialContext createInitialContext() throws NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty("java.naming.provider.url", "tcp://localhost:61616");
props.setProperty("queue.financeiro", "fila.financeiro");
InitialContext ctx = new InitialContext(props);
return ctx;
}
public void consomeMensagens() {
try {
Session sessao = conexao.createSession(false, 1);
MessageConsumer consumer = sessao.createConsumer(fila);
conexao.start();
consumer.setMessageListener(new MessageListener()
{
public void onMessage(Message message)
{
TextMessage textMessage = (TextMessage)message;
try {
System.out.println("Recebendo mensagem: " + textMessage.getText());
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
});
aguardeEnter();
sessao.close();
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
private void aguardeEnter() {
System.out.println("Aperte enter para finalizar");
Scanner s = new Scanner(System.in);
s.nextLine();
s.close();
}
}