Created
October 6, 2016 12:26
-
-
Save caandradeduarte/a527712241c1e1c6d86b171362b58b78 to your computer and use it in GitHub Desktop.
HornetQ Client Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hornetq.client; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.jms.Connection; | |
import javax.jms.JMSException; | |
import javax.jms.Session; | |
import org.hornetq.api.core.TransportConfiguration; | |
import org.hornetq.api.jms.HornetQJMSClient; | |
import org.hornetq.api.jms.JMSFactoryType; | |
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory; | |
import org.hornetq.core.remoting.impl.netty.TransportConstants; | |
import org.hornetq.jms.client.HornetQConnectionFactory; | |
public class HornetQClient { | |
private final HornetQConnectionFactory connectionFactory; | |
public HornetQClient(String host, int port) { | |
final Map<String, Object> connectionParams = new HashMap<String, Object>(); | |
connectionParams.put(TransportConstants.PORT_PROP_NAME, port); | |
connectionParams.put(TransportConstants.HOST_PROP_NAME, host); | |
final TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams); | |
connectionFactory = HornetQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, transportConfiguration); | |
} | |
public Session connect(String username, String password) throws JMSException { | |
Connection connection = connectionFactory.createConnection(username, password); | |
connection.setClientID("test-client" + String.format("%1$d", System.currentTimeMillis())); | |
connection.start(); | |
return connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hornetq.client; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import javax.jms.Topic; | |
import javax.jms.TopicSubscriber; | |
public class HornetQConsumer { | |
private static final String EXTERNAL_CLIENT = "external-client"; | |
private final Session session; | |
private TopicSubscriber subscriber; | |
public HornetQConsumer(Session session) { | |
this.session = session; | |
} | |
public TopicSubscriber createSubscriber(String topicName) throws JMSException { | |
Topic topic = session.createTopic(topicName); | |
subscriber = session.createDurableSubscriber(topic, EXTERNAL_CLIENT); | |
return subscriber; | |
} | |
public String receiveMessage() throws JMSException { | |
Message message = subscriber.receiveNoWait(); | |
String text = ((TextMessage) message).getText(); | |
subscriber.close(); | |
session.unsubscribe(EXTERNAL_CLIENT); | |
return text; | |
} | |
public void unsubscribe() throws JMSException { | |
subscriber.close(); | |
session.unsubscribe(EXTERNAL_CLIENT); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hornetq.client; | |
import javax.jms.JMSException; | |
import javax.jms.MessageProducer; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import javax.jms.Topic; | |
public class HornetQSender { | |
private final Session session; | |
public HornetQSender(Session session) { | |
this.session = session; | |
} | |
public void send(String topicName, String message) throws JMSException { | |
Topic topic = session.createTopic(topicName); | |
MessageProducer producer = session.createProducer(topic); | |
TextMessage messageToSend = session.createTextMessage(message); | |
producer.send(messageToSend); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>org.jboss.as</groupId> | |
<artifactId>jboss-as-jms-client-bom</artifactId> | |
<version>7.1.2.Final</version> | |
<type>pom</type> | |
</dependency> | |
<dependency> | |
<groupId>javax.jms</groupId> | |
<artifactId>jms</artifactId> | |
<version>1.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hornetq</groupId> | |
<artifactId>hornetq-jms-server</artifactId> | |
<version>2.4.0.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.javaee</groupId> | |
<artifactId>jboss-ejb-api</artifactId> | |
<version>3.0.0.CR2</version> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hornetq.client; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import javax.jms.JMSException; | |
import javax.jms.Session; | |
public class TopicTest { | |
private static final String HOST = "IP_NUMBER_OR_HOST"; | |
private static final int PORT = 5445; | |
private static final String USERNAME = "username"; | |
private static final String PASSWORD = "senha"; | |
public static void main(String[] args) throws IOException, JMSException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("*** Iniciando o teste de conexão com tópico ***"); | |
System.out.print("\nPor favor, informe o tópico que deseja testar (ex. tarifas.0000): "); | |
String topicName = reader.readLine(); | |
Session session = new HornetQClient(HOST, PORT).connect(USERNAME, PASSWORD); | |
HornetQConsumer consumer = new HornetQConsumer(session); | |
consumer.createSubscriber(topicName); | |
HornetQSender sender = new HornetQSender(session); | |
final String mensagem = String.format("Testando Tópico '%s'", topicName); | |
System.out.println("Enviando Mensagem: " + mensagem); | |
sender.send(topicName, mensagem); | |
System.out.println("Mensagem Enviada: " + mensagem); | |
System.out.println("Lendo Mensagem..."); | |
String mensagemRecebida = consumer.receiveMessage(); | |
System.out.println("Recebendo Mensagem: " + mensagemRecebida); | |
System.out.println("*** Teste Finalizado ***"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment