Fiz a implementação do QueueBrowser
porém mesmo alimentando o ActiveMQ
com mensagens, ele retorna que não há mensagens para exibir. Segue código abaixo:
Classe TesteProdutor
:
public class TesteProdutor {
public static void main(final String[] args) throws Exception {
final Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
properties.setProperty("java.naming.provider.url", "tcp://127.0.0.1:61616");
properties.setProperty("queue.financeiro", "fila.financeiro");
final InitialContext context = new InitialContext(properties);
final ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
final Connection connection = factory.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination fila = (Destination) context.lookup("financeiro");
final MessageProducer producer = session.createProducer(fila);
for (int i = 0; i < 1000; i++) {
final TextMessage message = session.createTextMessage("Iteração " + i);
producer.send(message);
}
session.close();
connection.close();
context.close();
}
}
Classe TesteConsumidor
:
public class TesteConsumidor {
@SuppressWarnings("resource")
public static void main(final String[] args) throws Exception {
final InitialContext context = new InitialContext();
final ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
final Connection connection = factory.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination fila = (Destination) context.lookup("financeiro");
final MessageConsumer consumer = session.createConsumer(fila);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(final Message message) {
final TextMessage textMessage = TextMessage.class.cast(message);
try {
System.out.println("Recebendo msg: " + textMessage.getText());
} catch (final JMSException e) {
e.printStackTrace();
}
}
});
new Scanner(System.in).nextLine();
connection.close();
context.close();
}
}
Classe TesteQueueBrowser
:
public class TesteQueueBrowser {
@SuppressWarnings("rawtypes")
public static void main(final String[] args) throws NamingException, JMSException {
final Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
properties.setProperty("java.naming.provider.url", "tcp://127.0.0.1:61616");
properties.setProperty("queue.financeiro", "fila.financeiro");
final InitialContext context = new InitialContext(properties);
final ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
final Connection connection = factory.createConnection();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = (Destination) context.lookup("financeiro");
final QueueBrowser browser = session.createBrowser((Queue) destination);
final Enumeration msgs = browser.getEnumeration();
if (!msgs.hasMoreElements()) {
System.out.println("Não há mensagens na fila");
} else {
while (msgs.hasMoreElements()) {
final TextMessage msg = (TextMessage) msgs.nextElement();
System.out.println("Mensagem: " + msg.getText());
}
}
}
}
O que estou fazendo de errado? O uso do QueueBrowser
tem de ser feita na mesma classe do produtor ao invés de usar em outra classe?