/** * Connects to the yamcs server. This method blocks until a connection is established or some * error has occurred, thus this should be called in a separate thread. Hornetq will try * indefinitely to establish the connection and also provides automatic re-connection afterwards. * * @throws Exception if the hornetq session could not be established due to some error */ public void connect() throws Exception { for (ConnectionListener cl : connectionListeners) { cl.connecting(connParams.getUrl()); } Map<String, Object> tcpConfig = new HashMap<String, Object>(); tcpConfig.put(TransportConstants.HOST_PROP_NAME, connParams.host); tcpConfig.put(TransportConstants.PORT_PROP_NAME, connParams.port); locator = HornetQClient.createServerLocatorWithoutHA( new TransportConfiguration(NettyConnectorFactory.class.getName(), tcpConfig)); locator.setInitialConnectAttempts(initialConnectAttempts); locator.setReconnectAttempts(reconnectAttempts); locator.setRetryInterval(retryInterval); locator.setRetryIntervalMultiplier(retryIntervalMultiplier); locator.setMaxRetryInterval(maxRetryInterval); locator.setAckBatchSize(ackBatchSize); sessionFactory = locator.createSessionFactory(); // TODO Use hornetq auth (like YamcsConnector), or keep anonymous connection? session = sessionFactory.createSession(false, true, true, preAcknowledge); session.addFailureListener(YamcsAckConnector.this); session.start(); for (ConnectionListener cl : connectionListeners) { cl.connected(connParams.getUrl()); } }
public void connectionFailed(HornetQException e, boolean failedOver) { if (failedOver) { logger.info("reconnected to yamcs: {}", e.getMessage()); for (ConnectionListener cl : connectionListeners) { cl.connected(connParams.getUrl()); } } else { logger.warn("connection to yamcs failed: {}", e.getMessage()); for (ConnectionListener cl : connectionListeners) { cl.connectionFailed(connParams.getUrl(), new YamcsException(e.getMessage(), e)); cl.log(e.getMessage()); } } }
/** * Creates a new connector to a yamcs server. * * @param yamcsUrl the url to connect to yamcs * @param instance the yamcs instance to use * @throws Exception if the provided url could not be parsed or contains an invalid hostname */ public YamcsAckConnector(final String yamcsUrl, final String instance) throws Exception { this.connParams = YamcsConnectData.parse(yamcsUrl); this.instance = instance; // default values for reconnection attempts initialConnectAttempts = -1; reconnectAttempts = -1; retryInterval = 3000; retryIntervalMultiplier = 2.0d; maxRetryInterval = 60000; // send acknowledges immediately ackBatchSize = 0; // do client-side acknowledgement preAcknowledge = false; dataConsumers = new LinkedList<ClientConsumer>(); connectionListeners = new LinkedList<ConnectionListener>(); // check for invalid hostnames, as it may be hidden in hornetq otherwise when the number of // reconnection attempts is set to unlimited try { Socket socket = new Socket(connParams.host, connParams.port); socket.close(); } catch (UnknownHostException e) { throw new Exception("invalid hostname in yamcs url: " + yamcsUrl); } catch (Exception e) { // ignore other exceptions } }