Skip to content

Commit

Permalink
fix startup issues
Browse files Browse the repository at this point in the history
  • Loading branch information
abmargb committed Dec 23, 2014
1 parent 1f20a83 commit aeb1132
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
79 changes: 47 additions & 32 deletions src/main/java/com/buddycloud/mediaserver/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package com.buddycloud.mediaserver;

import com.buddycloud.mediaserver.commons.MediaServerConfiguration;
import com.buddycloud.mediaserver.web.MediaServerApplication;
import com.buddycloud.mediaserver.xmpp.MediaServerComponent;
import com.buddycloud.mediaserver.xmpp.XMPPToolBox;
import org.jivesoftware.smack.*;
import java.util.Properties;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
Expand All @@ -33,12 +34,15 @@
import org.slf4j.LoggerFactory;
import org.xmpp.component.ComponentException;

import java.util.Properties;
import com.buddycloud.mediaserver.commons.MediaServerConfiguration;
import com.buddycloud.mediaserver.web.MediaServerApplication;
import com.buddycloud.mediaserver.xmpp.MediaServerComponent;
import com.buddycloud.mediaserver.xmpp.XMPPToolBox;

public class Main {
private static Logger LOGGER = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
Properties configuration = MediaServerConfiguration.getInstance()
.getConfiguration();

Expand All @@ -49,14 +53,7 @@ public static void main(String[] args) {
System.exit(1);
}

try {
while (!startXMPPToolBox(configuration)) {
Thread.sleep(5000);
}
} catch (Exception e) {
LOGGER.error("Error while starting XMPP client / component", e);
}

startXMPPToolBox(configuration);
keepAlive();
}

Expand Down Expand Up @@ -117,26 +114,39 @@ private static void setXMPPReplyTimeout(Properties configuration) {
MediaServerConfiguration.XMPP_REPLY_TIMEOUT));
SmackConfiguration.setPacketReplyTimeout(xmppReplyTimeout);
}

private static boolean startXMPPToolBox(Properties configuration) {

private static void startXMPPToolBox(Properties configuration)
throws InterruptedException {
setXMPPReplyTimeout(configuration);

XMPPConnection connection = createAndStartConnection(configuration);
addTraceListeners(connection);
LOGGER.info("Buddycloud Media Server XMPP client connection started!");

MediaServerComponent component;
try {
component = createXMPPComponent(configuration);
} catch (ComponentException e) {
return false;
}

XMPPConnection connection = null;

while (true) {
try {
connection = createAndStartConnection(configuration);
addTraceListeners(connection);
LOGGER.info("Buddycloud Media Server XMPP client connection started!");
break;
} catch (Exception e) {
LOGGER.error("Error while starting XMPP client", e);
}
Thread.sleep(30000);
}

MediaServerComponent component = null;

while (true) {
try {
component = createXMPPComponent(configuration);
LOGGER.info("Buddycloud Media Server XMPP component started!");
break;
} catch (Exception e) {
LOGGER.error("Error while starting XMPP component", e);
}
Thread.sleep(30000);
}

XMPPToolBox.getInstance().start(component, connection, configuration);

LOGGER.info("Buddycloud Media Server XMPP component started!");

return true;
}

private static MediaServerComponent createXMPPComponent(
Expand Down Expand Up @@ -211,6 +221,11 @@ private static XMPPConnection createAndStartConnection(
configuration.getProperty(MediaServerConfiguration.XMPP_CONNECTION_PASSWORD));
} catch (org.jivesoftware.smack.XMPPException e) {
LOGGER.error("XMPP connection coudn't be started", e);
try {
connection.disconnect();
} catch (Exception e2) {
// Do nothing, best effort
}
throw new com.buddycloud.mediaserver.commons.exception.XMPPException(e.getMessage(), e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ private void addField(Element xEl, String var, String type,
fieldEl.addAttribute("type", type);
fieldEl.addElement("value").setText(value);
}

@Override
public void postComponentShutdown() {
LOGGER.debug("Buddycloud Media XMPP component was disconnected.");
}

@Override
public void postComponentStart() {
LOGGER.debug("Buddycloud Media XMPP component is connected and ready to accept packets.");
}
}

0 comments on commit aeb1132

Please sign in to comment.