The technical principles of HornetQ core client framework in the Java class library
HornetQ is a reliable, high -performance open source message queue middleware. It uses the Java library to build a core client framework.How to realize the technical principles of this framework, this article will be explained from the following aspects and provides related Java code examples.
1. Connection management:
Hornetq's core client framework to ensure the connection stability and efficiency of the connection to the message server by connecting management.By creating and maintaining the connection pool, the client can reuse existing connections, and the connection can be re -established when the connection fails.Here are a simple sample code implemented using the HORNETQ connection manager:
import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.client.ClientSessionFactory;
import org.hornetq.api.core.client.ServerLocator;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
public class ConnectionManager {
private ServerLocator serverLocator;
private ClientSessionFactory sessionFactory;
public ConnectionManager() throws HornetQException {
serverLocator = HornetQClient.createServerLocator("tcp://localhost:61616");
sessionFactory = serverLocator.createSessionFactory();
}
public ClientSessionFactory getSessionFactory() {
return sessionFactory;
}
public void close() {
if (sessionFactory != null) {
sessionFactory.close();
}
if (serverLocator != null) {
serverLocator.close();
}
}
}
2. Message sending and receiving:
The Hornetq client framework provides a rich API for sending and receiving messages.The following is a simple sample code for sending and receiving messages using hornetq:
import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.client.ClientConsumer;
import org.hornetq.api.core.client.ClientMessage;
import org.hornetq.api.core.client.ClientProducer;
import org.hornetq.api.core.client.ClientSession;
import org.hornetq.api.core.client.ClientSessionFactory;
import org.hornetq.api.core.client.MessageHandler;
public class MessagingService {
private ConnectionManager connectionManager;
public MessagingService(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}
public void sendMessage(String destination, String text) throws HornetQException {
ClientSessionFactory sessionFactory = connectionManager.getSessionFactory();
try (ClientSession session = sessionFactory.createSession()) {
ClientProducer producer = session.createProducer(destination);
ClientMessage message = session.createMessage(true);
message.getBodyBuffer().writeString(text);
producer.send(message);
}
}
public void receiveMessage(String destination) throws HornetQException {
ClientSessionFactory sessionFactory = connectionManager.getSessionFactory();
try (ClientSession session = sessionFactory.createSession()) {
ClientConsumer consumer = session.createConsumer(destination);
consumer.setMessageHandler(new MessageHandler() {
@Override
public void onMessage(ClientMessage message) {
String text = message.getBodyBuffer().readString();
System.out.println("Received message: " + text);
}
});
session.start();
}
}
}
Third, the news durable:
The Hornetq client framework supports the persistence of messages to ensure that even when the message server fails or restarts, it can ensure the reliable transmission of the message.The framework saves messages into persistent storage through the corresponding persistence strategy.Here are a sample code that configures the configuration message:
import org.hornetq.api.config.ServerLocatorConfig;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
import org.hornetq.core.remoting.impl.netty.TransportConstants;
public class PersistenceConfig {
public static ServerLocatorConfig createServerLocatorConfig() {
ServerLocatorConfig config = new ServerLocatorConfig();
TransportConfiguration transport =
new TransportConfiguration(NettyConnectorFactory.class.getName());
config.setCallTimeout(5000);
config.setConnectionTTL(60000);
config.setConfirmationWindowSize(1024);
config.setMinLargeMessageSize(1024);
config.setPersistIDCache(true);
config.setPersistDeliveryCountBeforeDelivery(true);
config.setConnectionLoadBalancingPolicyClassName(
"org.hornetq.api.core.client.loadbalance.FirstElementConnectionLoadBalancingPolicy");
config.getParams().put(TransportConstants.HOST_PROP_NAME, "localhost");
config.getParams().put(TransportConstants.PORT_PROP_NAME, "61616");
config.getParams().put(TransportConstants.PORT_PROP_NAME, "61616");
return config;
}
}
The above is the introduction and example code of the technical principles of the HornetQ core client framework in the Java class library.By connecting management, message sending and receiving, and durable message, HornetQ provides reliable and high -performance message transmission capabilities, which is suitable for various distributed application scenarios.