All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.structr.cloud.Sender Maven / Gradle / Ivy

Go to download

Structr is an open source framework based on the popular Neo4j graph database.

The newest version!
/**
 * Copyright (C) 2010-2016 Structr GmbH
 *
 * This file is part of Structr .
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Structr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Structr.  If not, see .
 */
package org.structr.cloud;

import java.io.DataOutputStream;
import org.structr.cloud.message.Message;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 *
 */
public class Sender extends Thread {

	private static final Logger logger = Logger.getLogger(Sender.class.getName());

	private final BlockingQueue outputQueue = new ArrayBlockingQueue<>(10000);
	private DataOutputStream outputStream            = null;
	private CloudConnection connection               = null;

	public Sender(final CloudConnection connection, final DataOutputStream outputStream) {

		super("Sender of " + connection.getName());
		this.setDaemon(true);

		this.outputStream = outputStream;
		this.connection   = connection;

		// flush stream to avoid ObjectInputStream to be waiting indefinitely
		try {

			outputStream.flush();

		} catch (IOException ioex) {
			logger.log(Level.WARNING, "", ioex);
		}
	}

	@Override
	public void run() {

		while (connection.isConnected()) {

			try {

				while (!outputQueue.isEmpty()) {

					final Message message = outputQueue.poll();
					if (message != null) {

						message.serialize(outputStream);
						message.afterSend(connection);
					}
				}

				outputStream.flush();

			} catch (Throwable t) {

				connection.close();
			}

			try { Thread.yield(); } catch (Throwable t) {}
		}
	}

	public void send(final Message message) {

		try {
			outputQueue.put(message);

		} catch (InterruptedException iex) {
			logger.log(Level.WARNING, "", iex);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy