How to implement java production grade RabbitMQ consumer - java
I having a problem with the RabbitMQ Work Queue implementation. im current running it in Tomcat, and i have the following class constantly listerning to new task in the queue. But after a day or two, sudden it behaving strangely, where by the object DeliveryOK return by channel.queueDeclare(taskQueueName, isDurable, false, false, null); is always zero. (i print out this in the log below mentioning "Current poolSize").
But in Rabbit admin (./rabbitmqadmin list queues or the RabbitMq Admin portal) it always return a number greater than zero (say 1267 messages in the queue). And it will not reduce to zero until i restart the tomcat, the class below only able to detect that there are actually some messages in the queue.
Initially i thought that this class was terminated somehow, but it is able to consume those messages that newly arrive. It will not consume those 1267 messages that is left hanging inside the queue. For example messages 1267 in the queue, will not be consume until i restart tomcat.
From the code below, is it because buggy implementation or is there a better way to implement a queue consumer specifically for RabbitMQ? i have read a related stack post(Producer/Consumer threads using a Queue), but im not sure if it helps.
Also, is it true that this consumer implementation below will not survive a RunTimeException?
MqConsumer Class:
#Service
public class MqConsumer implements Runnable{
private static final Logger logger = LoggerFactory.getLogger(MqConsumer.class);
private final int MAX_ALERT_THRESHOLD = 10000;
#Autowired
private AsynchSystemConnections asynchSystemConnections;
public MqConsumer(){
}
#PostConstruct
private void init() {
(new Thread(new MqConsumer(asynchSystemConnections))).start();
}
public MqConsumer(AsynchSystemConnections asynchSystemConnections){
this.asynchSystemConnections = asynchSystemConnections;
}
#Override
public void run() {
logger.info("Execute Consumer instance...");
while (true) { // infinite loop until it die due server restart
boolean toSleep = consume(asynchSystemConnections);
if (toSleep){
logger.error("Sleeping for 1 second...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("", e);
}
}
}
}
private boolean consume(AsynchSystemConnections asynchSystemConnections) {
com.rabbitmq.client.Connection mqConnection = null;
Channel mqChannel = null;
DatasiftMq dMq = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(asynchSystemConnections.getMqServerHost());
mqConnection = factory.newConnection();
mqChannel = mqConnection.createChannel();
//consumePushInteractionJob method will forward to AsynchTwService.consume(connection, channel, AsynchTwService.PUSH_INTERACTION_QUEUE )
dMq = asynchSystemConnections.getAsynchService().consumePushInteractionJob(mqConnection, mqChannel);
int poolSize = asynchSystemConnections.getAsynchService().getPushInteractionQueueSize();
logger.info("Current poolSize: " + poolSize);
} catch(NullPointerException e) {
logger.error("", e);
if (dMq != null) {
try {
logger.error("Removing JSON with" + dMq.getLogHeader(dMq));
asynchSystemConnections.getAsynchService().ack(mqChannel, dMq.getDelivery());
logger.error("Removed JSON with" + dMq.getLogHeader(dMq));
} catch (IOException e1) {
logger.error("Remove JSON Failed: ", e);
}
}
return true;
} catch (IOException e) {
logger.error("Unable to create new MQ Connection from factory.", e);
return true;
} catch (InterruptedException e) {
logger.error("", e);
return true;
} catch (ClassNotFoundException e) {
logger.error("", e);
return true;
} catch (Exception e) {
logger.error("Big problem, better solve this fast!!", e);
asynchSystemConnections.getNotificationService().notifySystemException(null, e);
return true;
} finally {
try {
asynchSystemConnections.getAsynchService().ack(mqChannel, dMq.getDelivery());
asynchSystemConnections.getAsynchService().disconnect(mqConnection, mqChannel);
} catch (IOException e) {
logger.error("", e);
}
}
return false;
}
AsynchTwService Class:
#Service("asynchTwService")
public class AsynchTwService implements AsynchService {
static final String FAVOURITE_COUNT_QUEUE = "favourite_count_queue";
static final String FRIENDS_FOLLOWERS_QUEUE = "friends_followers_queue";
static final String DIRECT_MESSAGE_RECEIVE_QUEUE = "direct_message_receive_queue";
static final String PUSH_INTERACTION_QUEUE = "push_interaction_queue";
private static String mqServerHost;
private static final Logger logger = LoggerFactory.getLogger(AsynchTwService.class);
private static final boolean isDurable = true;
private boolean autoAck = false;
private ConcurrentHashMap<String, Integer> currentQueueSize = new ConcurrentHashMap<String, Integer>();
#Override
public Connection getConnection() throws IOException{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(mqServerHost);
return factory.newConnection();
}
#Override
public void produce(Connection connection, Channel channel, Object object, String taskQueueName) throws IOException {
sendToQueue(connection, channel, object, taskQueueName);
}
#Override
public QueueItem consume(Connection connection, Channel channel, String taskQueueName) throws IOException, InterruptedException, ClassNotFoundException{
Serializer serializer = new Serializer();
try {
Delivery delivery = listenFromQueue(connection, channel, taskQueueName);
Object messageObj = serializer.toObject(delivery.getBody());
QueueItem queueItem = (QueueItem)messageObj;
queueItem.setDelivery(delivery);
return queueItem;
} catch (InterruptedException e) {
throw e;
} catch (ClassNotFoundException e) {
logger.error("Unable to serialize the message to QueueItem object", e);
throw e;
}
}
#Override
public int getQueueSize(String taskQueueName){
return this.currentQueueSize.get(taskQueueName);
}
private Delivery listenFromQueue(Connection connection, Channel channel, String taskQueueName) throws IOException, InterruptedException, ClassNotFoundException{
try {
DeclareOk ok = channel.queueDeclare(taskQueueName, isDurable, false, false, null);
currentQueueSize.put(taskQueueName, ok.getMessageCount());
logger.info("Queue ("+ taskQueueName + ") has items: " +ok.getMessageCount());
} catch (IOException e) {
// TODO Auto-generated catch block
}
logger.info(" [*] Consuming "+taskQueueName+" message...");
QueueingConsumer consumer = new QueueingConsumer(channel);
try {
channel.basicConsume(taskQueueName, autoAck, consumer);
} catch (IOException e) {
logger.error("", e);
}
try {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
return delivery;
} catch (ShutdownSignalException e) {
logger.error("Unable to retrieve message from Queue", e);
throw e;
} catch (ConsumerCancelledException e) {
logger.error("Unable to retrieve message from Queue", e);
throw e;
} catch (InterruptedException e) {
logger.error("Unable to retrieve message from Queue", e);
throw e;
}
}
private void sendToQueue(Connection connection, Channel channel, Object object, String taskQueueName) throws IOException{
//Initialization, create Message Queue broker connection
try{
channel.queueDeclare(taskQueueName, isDurable, false, false, null);
}catch(IOException e) {
logger.error(e.getMessage());
logger.error("Error create Message Queue connection for queue name:" + taskQueueName, e);
throw e;
}
//send message to broker
try {
long start = System.currentTimeMillis();
Serializer serializer = new Serializer();
logger.info("Sending Twitter QueueItem to Message Queue...");
channel.basicPublish("", taskQueueName, MessageProperties.PERSISTENT_TEXT_PLAIN,
serializer.toBytes(object));
logger.info("Queue successfully sent, process took: " + (System.currentTimeMillis()-start)+ "ms");
} catch (IOException e) {
logger.error("Error while sending object to queue : " + taskQueueName, e);
throw e;
}
}
public static String getMqServerHost() {
return mqServerHost;
}
public static void setMqServerHost(String mqServerHost) {
AsynchTwService.mqServerHost = mqServerHost;
}
#Override
public void disconnect(Connection connection, Channel channel) throws IOException{
try {
if (channel != null){
if (channel.isOpen()){
channel.close();
}
}
if (connection != null){
if (connection.isOpen()){
connection.close();
}
}
logger.debug("MQ Channel Disconnected");
} catch (IOException e) {
throw e;
}
}
#Override
public void ack(Channel channel, QueueingConsumer.Delivery delivery) throws IOException {
// this is made as another method call is to avoid Ack too fast un intentionally
try {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
logger.info("[x] acked" );
} catch (IOException e) {
logger.error("Unable Acknowledge Queue Message", e);
throw e;
}
}
#Override
public DatasiftMq consumeDatasiftInteraction(Connection connection, Channel channel,
String taskQueueName) throws IOException, InterruptedException, ClassNotFoundException {
Serializer serializer = new Serializer();
try {
Delivery delivery = listenFromQueue(connection, channel, taskQueueName);
Object messageObj = serializer.toObject(delivery.getBody());
DatasiftMq dto = (DatasiftMq)messageObj;
dto.setDelivery(delivery);
return dto;
} catch (InterruptedException e) {
throw e;
} catch (ClassNotFoundException e) {
logger.error("Unable to serialize the message to DatasiftDTO object", e);
throw e;
}
}
#Override
public void reQueue(Channel channel, Delivery delivery) throws IOException {
try {
channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
logger.info("[x] Nacked" );
} catch (IOException e) {
logger.error("Unable Acknowledge Queue Message", e);
throw e;
}
}
}
Seems like you are missing some basics here.
Taken from here and some code of mine.
Setting up the connection outside of the consumer thread:
//executed once
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("someHost");
factory.setUsername("user");
factory.setPassword("pass");
Connection connection = factory.newConnection();
What you have to do inside your thread:
//Consumer - executed in a Thread
QueueingConsumer consumer = new QueueingConsumer(connection.createChannel());
boolean autoAck = false;
channel.basicConsume("hello", autoAck, consumer);
while (!Thread.current().isInterrupted())) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
//...
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
In general I do still recommand you check out the spring-amqp library it integrates perfectly.
Related
StreamCorruptedException after ~1000 transferred objects
I want to transfer objects (AssignmentListener) from one Java Server to 5 Java Clients. Therefore I wrote a method to send out the message: private void sendMessage(AssignmentListener listener, int[] subpartitionIndices){ boolean success = false; int failCount = 0; // retry for the case of failure while(!success && failCount < 10) { try { // get the stored socket & stream if stored if(listener.getSocket() == null) { if (localMode) { listener.setSocket(new Socket("localhost", listener.getPort())); } else { listener.setSocket(new Socket(listener.getIp(), listener.getPort())); } listener.setOutputStream(new ObjectOutputStream(listener.getSocket().getOutputStream())); } AssignmentListenerMessage assignmentListenerMessage = new AssignmentListenerMessage(subpartitionIndices); System.out.println("Sending " + assignmentListenerMessage); listener.getOutputStream().writeObject(assignmentListenerMessage); listener.getOutputStream().flush(); success = true; } catch (IOException se) { se.printStackTrace(); System.err.println("Failed to forward " + Arrays.toString(subpartitionIndices) + " to " + listener); failCount++; } } } On the client side, I have the following: public void run() { String mode = "remote"; if(localMode) mode = "local"; // we need to register this listener at at the OverpartitioningManager if(register(isLocalRequest)) System.out.println("Registered AssignmentListenerServer for index "+subpartitionIndex+" at ForwardingServer - "+mode); running = true; while (running) { try { socket = serverSocket.accept(); // Pass the socket to the RequestHandler thread for processing RequestHandler requestHandler = new RequestHandler( socket ); requestHandler.start(); } catch (SocketException se) { se.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } class RequestHandler extends Thread { private Socket socket; RequestHandler(Socket socket) { this.socket = socket; } #Override public void run() { try { System.out.println("Received a connection"); // Get input and output streams inStream = new ObjectInputStream(socket.getInputStream()); //outStream = new DataOutputStream(socket.getOutputStream()); AssignmentListenerMessage incomingMessage = null; while(socket.isBound()) { try { incomingMessage = (AssignmentListenerMessage) inStream.readObject(); }catch (StreamCorruptedException sce){ System.out.println("Failed to read AssignmentMessage from Stream, but will try again... (no ack)"); sce.printStackTrace(); continue; } // do stuff with the message } // Close our connection inStream.close(); socket.close(); System.out.println("Connection closed"); } catch (Exception e) { e.printStackTrace(); } } } This works multiple times, but at one point I get the following exception: java.io.StreamCorruptedException: invalid type code: 00 Does anyone have an idea or any other performance improvement for what I'm doing? Thanks.
Multithreaded server randomly throws java.net.SocketTimeoutException: Read timed out
I have a multithreaded tcp server, that handles multiple clients. Each client has its thread on the serverside that keeps the socket connection. Everything theoretically works fine for many minutes, but at rare occasions, while having multiple clients connected, the following happens: One of the clients sends a tcp packet to the server and the serverside read times out. I have found many questions, that tackle read timeouts on the clientside, but in my case, this never happens. In my case, the server times out on a read when receiving a packet from a client. My question is, why and how can this happen and what can I do to handle this problem? here is my server listener: public class GameServerTCP extends Thread { //TCP private ServerSocket serverSocket; public Server server; public int amountOfTCPConnections = 0; ClassLoader classLoader = getClass().getClassLoader(); File myFile = new File(classLoader.getResource("Sprites/sprite_sheet.png").getFile()); public GameServerTCP(Server game) { this.server = game; //TCP try { serverSocket = new ServerSocket(6336); } catch (IOException e) { e.printStackTrace(); } } public void run() { while(true) { //TCP Socket socket = null; try { socket = serverSocket.accept(); Toolkit.getDefaultToolkit().beep(); System.out.println(socket.getRemoteSocketAddress() + " has connected to server."); } catch (Exception e) { e.printStackTrace(); } new TCPConnection(socket, this); amountOfTCPConnections++; if (amountOfTCPConnections > 500) { System.err.println("Too many clients error! (unsolved)"); server.frame.dispatchEvent(new WindowEvent(server.frame, WindowEvent.WINDOW_CLOSING)); } } } } here is my server thread that hold each single connection: public class TCPConnection implements Runnable { Socket socket; private Thread thread; private boolean isRunning = false; public GameServerTCP serverTCP; private String gamename = "-1"; public String username; /** * This is the future! * Contains an exact imprint of the player of client side. * Cheats can be detected here. */ private PlayerMP playerMP; String clientSentence; TCPConnection(Socket socket, GameServerTCP serverTCP) { this.socket = socket; this.serverTCP = serverTCP; isRunning = true; thread = new Thread(this); thread.start(); } public synchronized void closeConnection() { if (MasterConnections.connectionsTCP.containsKey(getUniqueConnectionIdentifier())) MasterConnections.connectionsTCP.remove(getUniqueConnectionIdentifier()); if (this.username != null && MasterConnections.currentlyLoggedOnAccounts.contains(this.username)) MasterConnections.currentlyLoggedOnAccounts.remove(this.username); if (this.gamename != null && serverTCP.server.games.containsKey(this.gamename)) { Level game = serverTCP.server.games.get(this.gamename); for (String p : game.playersInLevel) { if (p.equals(getUniqueConnectionIdentifier())) { game.playersInLevel.remove(p); System.out.println(this.username + " has been been removed from game " + this.gamename + "."); } } PacketTCP02LeaveGame tellOthersPacket = new PacketTCP02LeaveGame(this.gamename, this.username); game.writeDataTCPToAllPlayersInThisLevel(tellOthersPacket); } try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(socket.getRemoteSocketAddress() + " has been disconnected from server."); this.serverTCP.amountOfTCPConnections--; this.stop(); } public String getUniqueConnectionIdentifier() { return socket.getInetAddress() + ":" + socket.getPort(); } public String generateUniqueUDPConnectionIdentifier(InetAddress inetAddess, int udpPort) { System.out.println("uuc created: "); System.out.println(inetAddess + "/" + udpPort); return inetAddess + ":" + udpPort; } public void run() { //version check first PacketTCP00VersionCheck packetVersionCheck = new PacketTCP00VersionCheck(serverTCP.server.getVersion()); if (MasterConnections.connectionsTCP.containsKey(getUniqueConnectionIdentifier())) { this.closeConnection(); } else { MasterConnections.connectionsTCP.put(getUniqueConnectionIdentifier(), this); packetVersionCheck.writeData(this); } BufferedReader inFromClient; try { inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (IOException e1) { e1.printStackTrace(); closeConnection(); return; } while(isRunning) { try { clientSentence = inFromClient.readLine(); if (clientSentence == null) { inFromClient.close(); closeConnection(); } else { System.out.println("tcprec -> " + (new Date(System.currentTimeMillis())) + " -> " + this.username + " -> " + clientSentence); this.parsePacket(clientSentence.getBytes()); } } catch (SocketTimeoutException ste) { /** * TODO: */ ste.printStackTrace(); System.err.println("YOU CAN DO SOMETHING HERE!!!!!!!"); closeConnection(); } catch (Exception e) { e.printStackTrace(); closeConnection(); } } } public void stop() { isRunning = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } And here is my client: public class GameClientTCP extends Thread { public String gamestate = "logged out"; private Game game; public Socket tcpSocket; public boolean isRunning = false; private String serverSentence; public boolean hasBeenStarted = false; public int boundUDPPort = -1; public static String[] characters = new String[5]; public static boolean charactersAreLoaded = false; private PrintWriter toServer; public GameClientTCP(Game game, String ipAddress) { this.game = game; } public boolean tryConnect() { try { tcpSocket = new Socket(); tcpSocket.connect(new InetSocketAddress(Settings.SERVER_ADDRESS, 6336), 1000); System.out.println("Just connected to " + tcpSocket.getRemoteSocketAddress()); game.getSocketClientUDP().prepareBeforeStart(); game.getSocketClientUDP().start(); return true; } catch (UnknownHostException e1) { try { tcpSocket.close(); } catch (IOException e) { GameError.appendToErrorLog(e); return false; } return false; } catch (IOException e1) { try { tcpSocket.close(); } catch (IOException e) { GameError.appendToErrorLog(e); return false; } GameError.appendToErrorLog(e1); return false; } } public void run() { BufferedReader fromServer; try { fromServer = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream())); toServer = new PrintWriter(tcpSocket.getOutputStream(),true); } catch (IOException e1) { GameError.appendToErrorLog(e1); return; } while(isRunning) { try { serverSentence = fromServer.readLine(); //System.out.println("Received: " + serverSentence); if (serverSentence != null) this.parsePacket(serverSentence.getBytes()); } catch(UnknownHostException ex) { GameError.appendToErrorLog(ex); } catch(IOException e){ GameError.appendToErrorLog(e); } catch(Exception e) { GameError.appendToErrorLog(e); } } } public void sendMessageToServer(String message) { try { toServer.println(message); toServer.flush(); } catch (Exception e) { GameError.appendToErrorLog(e); System.exit(-1); } } } I hope to find out more about this issue, please help! :) EDIT: It may be important to say, that while my program is running, it can occur, that there are no tcp packets sent over a longer period of time. The timeout always happens, when i dont send any packets for at least 20 or 30 minutes and then when i send one again, another client times out.
As it turned out, tcp sockets, that are not used longer than a certain amount of time will be kind of destroyed by peers and therefore lose their connection. I solved my issue, by sending a nearly empty tcp packet every minute to make it clear to all programs and services, that these sockets are alive!
read packet from tcp socket in java with acknoledgement
We are creating a java listener to read multiple device data those are configured on particular server ip and port.Device following below rule. device send a login packet. server will return ack packet in response. after receive ack device will send information packet. server reads that data. on last step we stuck, we are sending the ack but cant get the information packet back from device(though we check the generated ack through opensource sofware).For ref we are attaching code.(if we remove while(true) than get login packet but after that socket connection will close and again device will send login packet but if we keep it then we dont get any packet) //--------------Main class------------------------------------------ public class Main { public static void main(String[] args) { Server server = new Server(listen_port, pool_size, pm); new Thread(server).start(); logger.info("Server Started ....."); } } //-------------------------------------------------------------- public class Server implements Runnable { private ServerSocket serverSocket = null; public void run() { this.m_stop = false; while (!this.m_stop) try { this.m_pool.execute(new Handler(this.serverSocket.accept())); } catch (IOException e) { LOGGER.debug("Unable to accept connection ", e); } } } //-------------------------------------------------------------- public class Handler implements Runnable { private Socket m_clientSocket; private String imei; public Handler(Socket socket) { this.m_clientSocket = socket; } public void run() { DataOutputStream clientDataOS = null; DataInputStream clientDataIS = null; try { logger.info("data is coming"); m_clientSocket.setSoTimeout(300000); clientDataIS = new DataInputStream(this.m_clientSocket.getInputStream()); clientDataOS = new DataOutputStream(this.m_clientSocket.getOutputStream()); while (true) { String pkt = ""; logger.info("Waiting for input strem"); byte[] byte_pkt = IOUtils.toByteArray(clientDataIS); logger.info("Got input stream"); for (byte b : byte_pkt) { pkt += String.format("%02X ", b); } logger.info(pkt); if (byte_pkt.length > 0) { logger.info(""); if (Byte.valueOf(byte_pkt[3]) == 1) { imei = "xyz"; logger.info("login packet"); byte[] rep_pkt = Utils.getReceptionPacket(byte_pkt);//send back to device clientDataOS.write(rep_pkt); clientDataOS.flush(); } else if (Byte.valueOf(byte_pkt[3]) == 34) { logger.info("information packet"); Utils.processPackets(byte_pkt); } else { logger.info("Unkown packet format"); } logger.info(imei); } else { logger.info("InputStream is empty."); } } } catch (SocketException se) { logger.error("Failure on reading data", se); } catch (IOException e) { logger.error("Failure on reading data", e); } catch (Exception e) { logger.error("Error while processing data", e); } finally { try { IOUtils.closeQuietly(clientDataOS); IOUtils.closeQuietly(clientDataIS); this.m_clientSocket.close(); } catch (IOException e) { logger.debug("Error when sending out response ::", e); } } } }
ActiveMQ + MQTT + subscribe to "ActiveMQ.Advisory.Connection"
This is the context: A Java application subscribes to the Topic "ActiveMQ.Advisory.Connection" from an ActiveMQ 5.9.1 via MQTT (Paho 0.4.0): public class SupervisorMqttClient implements MqttCallback { private MqttClient client = null; private MemoryPersistence persistence = null; private MqttConnectOptions connOpts = null; private final int STATUS_OK = 0; private final int STATUS_ERROR = 1; private String mqttServer = null; private String clientId = null; private int status = STATUS_OK; public SupervisorMqttClient() { try { this.init(); } catch (MqttException e) { Logger.error(e.getLocalizedMessage()); Logger.debug(e); } } private void init() throws MqttException { Properties props = PropertiesManager.getInstance("supervisor"); mqttServer = props.getProperty("supervisor.mqtt.server"); String supervisorID = props.getProperty("supervisor.mqtt.client.number"); clientId = Supervisor.APP_NAME+"-"+supervisorID; connOpts = new MqttConnectOptions(); connOpts.setKeepAliveInterval(30); connOpts.setCleanSession(true); // important non-durable persistence = new MemoryPersistence(); client = new MqttClient(mqttServer, clientId, persistence); connectAndSubscribe(); } private void connectAndSubscribe() throws MqttSecurityException, MqttException { try { client.connect(connOpts); client.setCallback(this); client.subscribe("ActiveMQ/Advisory/Connection"); } catch (MqttSecurityException e) { Logger.error(e.getLocalizedMessage()); Logger.debug(e); } catch (MqttException e) { Logger.error(e.getLocalizedMessage()); Logger.debug(e); processError(e); } } public void publish(String orderType, JSONObject jsonExtraData) { if (status == STATUS_ERROR) { connectAndSubscribe(); } if (status == STATUS_OK) { // some code here } } #Override public void connectionLost(Throwable err) { Logger.info("Connection lost"); } #Override public void deliveryComplete(IMqttDeliveryToken arg0) { Logger.info("deliveryComplete"); } #Override public void messageArrived(String topic, MqttMessage msg) throws Exception { System.out.println("MQTT Mesage Arrived[" + topic + "] Msg[" + msg.toString() + "]"); } private void processError(MqttException e) { status = STATUS_ERROR; try { if (client.isConnected()) { Logger.error("disconnecting"); client.disconnect(); } } catch (MqttException ex) { Logger.error(ex.getLocalizedMessage()); Logger.debug(ex); } } } The connection with ActiveMQ is established fine. This topic offers information about the connections (open/close) in the ActiveMQ, but my problem is that messages I catch are empty: MQTT Mesage Arrived[ActiveMQ/Advisory/Connection] Msg[] Is there any way to catch them using MQTT? or I should use JMS for that? Thanks, Jon Ander.
The question would be what do you want the MQTT client to receive on the Advisory topic as the message body. The advisories generally include much of the information as message properties however those cannot be mapped to MQTT as MQTT messages don't have properties. The body of the Connection advisory is a copy of the ConnectionInfo object that was used to create the connection. On the MQTT side there is not much you could do with that as all you would receive would be the serialized bytes of that object which you wouldn't be able to do anything with.
Connect to remote port(Server) from thread in polling
I want to connect to a remote server from thread and keep sending strings. If the connection gets refused the thread should keep polling the port until the server is up again. How can I handle this exception and keep my thread fro crashing? The server may not be up for long time but thread should run indefinitely. public void SendMessage(String message){ try { socket = new Socket(actuatorAddress, destPort.get()); outToServer = socket.getOutputStream(); out = new DataOutputStream(outToServer); out.flush(); out.write(message.getBytes()); } catch (IOException ex) { System.out.println(ex.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } } I changed some part of the code as below. For first time called Connect function and then subsequently called Send Message function through thread. The delay added to reconnecting helped reduce time lag recurred due to connecting to non existing server. Still think that there might be a better solution to the basic problem. public boolean ConnectToActuator() { try { if(actuatorAddress.isReachable(2000)){ socket = new Socket(); socket.setPerformancePreferences(1, 2, 0); socket.setTcpNoDelay(false); socket.setSendBufferSize(32); socket.connect(new InetSocketAddress(actuatorAddress, destPort.get())); outToServer = socket.getOutputStream(); out = new DataOutputStream(outToServer); connected = true; disconnectedTimeout = 0; } }catch (ConnectException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); }catch (IOException ex) { connected = false; System.out.println(ex.getMessage()); } return connected; } public boolean SendToActuator(String message) { if(connected == false){ //socket.isOutputShutdown() disconnectedTimeout++; if(disconnectedTimeout>20){ disconnectedTimeout = 0; ConnectToActuator(); } else { return connected; } } try { out.flush(); out.writeBytes(message); disconnectedTimeout = 0; connected = true; } catch (UnknownHostException uhe) { connected = false; System.out.println(uhe.getMessage()); } catch (IOException ioe) { connected = false; System.out.println(ioe.getMessage()); } return connected; }
Given the following constraints in the comments: Try to send the message to one of the 10 servers. If none of the servers are available to receive the message, discard the message. What you actually want to do is: Iterate through a list of server addresses Attempt to send a message to each of them Break out of the loop right away if successful Catch any errors on connection failure and try the next server Here's an example class that will run through that scenario. import java.io.DataOutputStream; import java.io.OutputStream; import java.net.Socket; public class MessageSender { private static final Integer destPort = 1234; private static final String[] serverAddresses = { "address1", "address2", "address3" // Etc.... }; public Boolean SendMessage(String message) { Boolean messageSentSuccessfully = false; for (String addy : serverAddresses) { messageSentSuccessfully = SendMessageToServer(addy, message); if (messageSentSuccessfully) { break; } } return messageSentSuccessfully; } private Boolean SendMessageToServer(String serverAddress, String message) { Boolean messageSent = false; try { Socket dataSocket = new Socket(serverAddress, destPort); OutputStream outToServer = dataSocket.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.write(message.getBytes()); out.flush(); messageSent = true; } catch (Exception e) { System.out.println(e); } return messageSent; } } Hope that helps.
Pseudo: while(true){ if(connect()) DoClientConnectedStuff(); sleep(reconnectTimeout); };
please try below changes. if your connection refuses it will wait for 2s(2000ms) and then again try to connect with server. if connection being successful it will take outputstream, write data in a while loop and flush the data. public void createSocketConnection() throws IOException { socket = new Socket(actuatorAddress, destPort.get()); if(socket!=null) { outToServer = socket.getOutputStream(); out = new DataOutputStream(outToServer); } } public void SendMessage(String message){ boolean isRunning=false; try { createSocketConnection(); isRunning=true; while(isRunning) { out.write(message.getBytes()); out.flush(); } } catch (java.net.ConnectException conExcp) { System.out.println(ex.getMessage()); try{ Thread.sleep(2000); }catch(Exception ee){} } catch (IOException ex) { System.out.println(ex.getMessage()); } }