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

brooklyn.entity.messaging.activemq.ActiveMQBrokerImpl Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.entity.messaging.activemq;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.entity.basic.Entities;
import brooklyn.entity.java.UsesJmx;
import brooklyn.entity.messaging.jms.JMSBrokerImpl;
import brooklyn.entity.proxying.EntitySpecs;
import brooklyn.event.feed.jmx.JmxAttributePollConfig;
import brooklyn.event.feed.jmx.JmxFeed;

import com.google.common.base.Functions;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Predicates;
/**
 * An {@link brooklyn.entity.Entity} that represents a single ActiveMQ broker instance.
 */
public class ActiveMQBrokerImpl extends JMSBrokerImpl implements ActiveMQBroker {
	private static final Logger log = LoggerFactory.getLogger(ActiveMQBrokerImpl.class);

    private volatile JmxFeed jmxFeed;

    public ActiveMQBrokerImpl() {
        super();
    }

	public void setBrokerUrl() {
		setAttribute(BROKER_URL, String.format("tcp://%s:%d", getAttribute(HOSTNAME), getAttribute(OPEN_WIRE_PORT)));
	}
	
    public Integer getJmxPort() {
        return !isJmxEnabled() ? Integer.valueOf(-1) : getAttribute(UsesJmx.JMX_PORT);
    }
    
    public Integer getOpenWirePort() {
        return getAttribute(OPEN_WIRE_PORT);
    }
    
    public boolean isJmxEnabled() {
        return Boolean.TRUE.equals(getConfig(USE_JMX));
    }

    @Override
	public ActiveMQQueue createQueue(Map properties) {
		ActiveMQQueue result = addChild(EntitySpecs.spec(ActiveMQQueue.class).configure(properties));
        Entities.manage(result);
        result.create();
        return result;
	}

    @Override
	public ActiveMQTopic createTopic(Map properties) {
		ActiveMQTopic result = addChild(EntitySpecs.spec(ActiveMQTopic.class).configure(properties));
        Entities.manage(result);
        result.create();
        return result;
	}

    @Override     
    protected void connectSensors() {
        setAttribute(BROKER_URL, String.format("tcp://%s:%d", getAttribute(HOSTNAME), getAttribute(OPEN_WIRE_PORT)));
        
        String brokerMbeanName = "org.apache.activemq:BrokerName=localhost,Type=Broker";
        
        jmxFeed = JmxFeed.builder()
                .entity(this)
                .period(500, TimeUnit.MILLISECONDS)
                .pollAttribute(new JmxAttributePollConfig(SERVICE_UP)
                        .objectName(brokerMbeanName)
                        .attributeName("BrokerId")
                        .onSuccess(Functions.forPredicate(Predicates.notNull()))
                        .onError(Functions.constant(false)))
                .build();
    }

    @Override
    public void disconnectSensors() {
        super.disconnectSensors();
        if (jmxFeed != null) jmxFeed.stop();
    }

	@Override
    protected ToStringHelper toStringHelper() {
        return super.toStringHelper().add("openWirePort", getAttribute(OPEN_WIRE_PORT));
    }

    @Override
    public Class getDriverInterface() {
        return ActiveMQDriver.class;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy