Skip to content
This repository has been archived by the owner on Feb 11, 2019. It is now read-only.

Commit

Permalink
switched to JMXConnector factory instead of MBeanServerConnectionFact…
Browse files Browse the repository at this point in the history
…oryBean injection
  • Loading branch information
Dominik Mengelt committed Oct 10, 2014
1 parent 77d44cd commit 365636b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 51 deletions.
23 changes: 6 additions & 17 deletions src/main/java/ch/filecloud/queuemonitor/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.filecloud.queuemonitor;

import ch.filecloud.queuemonitor.common.QmonEnvironment;
import ch.filecloud.queuemonitor.common.QmonEnvironmentConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand All @@ -13,10 +12,9 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.jmx.support.MBeanServerConnectionFactoryBean;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.MalformedURLException;

/**
* Created by domi on 7/21/14.
Expand All @@ -33,20 +31,6 @@ public class App {
@Value("#{ systemProperties['" + QMON_CONFIG +"'] }")
private String qmonConfig;

@Bean
protected MBeanServerConnectionFactoryBean getMBeanServerConnectionFactoryBean() throws MalformedURLException {
MBeanServerConnectionFactoryBean mBeanServerConnectionFactoryBean = new MBeanServerConnectionFactoryBean();

QmonEnvironmentConfiguration qmonEnvironmentConfiguration = getQmonEnvironmentConfiguration();
QmonEnvironment qmonEnvironment = qmonEnvironmentConfiguration.getFirst();

LOGGER.info("Using JMX URL " + qmonEnvironment.getJmxRemoteUrl());

mBeanServerConnectionFactoryBean.setServiceUrl(qmonEnvironment.getJmxRemoteUrl());
mBeanServerConnectionFactoryBean.setConnectOnStartup(false);
return mBeanServerConnectionFactoryBean;
}

@Bean
protected QmonEnvironmentConfiguration getQmonEnvironmentConfiguration() {
try {
Expand All @@ -70,6 +54,11 @@ public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
return mappingJackson2HttpMessageConverter;
}

@PostConstruct
public void postConstruct() {

}

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.filecloud.queuemonitor.client;

import ch.filecloud.queuemonitor.common.QmonEnvironment;
import ch.filecloud.queuemonitor.common.QmonEnvironmentConfiguration;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.HashMap;

/**
* Created by domi on 10/10/14.
*/
@Component
public class JmxConnectionClient {

@Inject
private QmonEnvironmentConfiguration qmonEnvironmentConfiguration;

public MBeanServerConnection get() {

try {
QmonEnvironment qmonEnvironment = qmonEnvironmentConfiguration.getCurrent();
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(qmonEnvironment.getJmxRemoteUrl()), new HashMap<String, Object>());

return connector.getMBeanServerConnection();
} catch (Exception e) {
// throw a nice custom exception here
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ public class QmonEnvironmentConfiguration {
private static final String QMON_DEFAULT_CONFIG_JSON = "qmonDefaultConfig.json";

private List<QmonEnvironment> environments;
private QmonEnvironment currentEnvironment;

public QmonEnvironment getFirst() {
return environments.get(0);
public QmonEnvironment getCurrent() {
return currentEnvironment;
}

public QmonEnvironment get(String environmentName) {
for(QmonEnvironment qmonEnvironment : environments) {
if(qmonEnvironment.getName().equals(environmentName)) {
return qmonEnvironment;
}
}

return getFirst();
public void setCurrent(String environmentName) {
currentEnvironment = get(environmentName);
}

public List<QmonEnvironment> getAll() {
Expand All @@ -44,6 +39,17 @@ public List<QmonEnvironment> getAll() {

private void sort() {
Collections.sort(environments);
currentEnvironment = environments.get(0);
}

private QmonEnvironment get(String environmentName) {
for(QmonEnvironment qmonEnvironment : environments) {
if(qmonEnvironment.getName().equals(environmentName)) {
return qmonEnvironment;
}
}

return getCurrent();
}

public static QmonEnvironmentConfiguration create(String filename) throws IOException {
Expand Down Expand Up @@ -72,6 +78,4 @@ private static QmonEnvironmentConfiguration createInternal(String configJson) {
qmonEnvironmentConfiguration.sort();
return qmonEnvironmentConfiguration;
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.filecloud.queuemonitor.service;

import ch.filecloud.queuemonitor.client.JmxConnectionClient;
import org.hornetq.api.core.management.ObjectNameBuilder;
import org.hornetq.api.jms.management.JMSServerControl;
import org.springframework.jmx.support.MBeanServerConnectionFactoryBean;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
Expand All @@ -16,12 +16,12 @@
public abstract class ControlService {

@Inject
protected MBeanServerConnectionFactoryBean mBeanServerConnectionFactoryBean;
protected JmxConnectionClient jmxConnectionClient;

protected JMSServerControl getJMSServerControl() {
try {
ObjectName objectName = ObjectNameBuilder.DEFAULT.getJMSServerObjectName();
return MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnectionFactoryBean.getObject(), objectName, JMSServerControl.class, false);
return MBeanServerInvocationHandler.newProxyInstance(jmxConnectionClient.get(), objectName, JMSServerControl.class, false);
} catch (Exception e) {
throw new IllegalArgumentException("Object name not found!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import ch.filecloud.queuemonitor.service.ControlService;
import ch.filecloud.queuemonitor.service.exception.QmonConnectionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hornetq.api.core.management.ObjectNameBuilder;
import org.hornetq.api.jms.management.JMSQueueControl;
import org.springframework.stereotype.Component;
Expand All @@ -19,7 +17,6 @@
@Component
public class QueueControlService extends ControlService {

private static Log LOGGER = LogFactory.getLog(QueueControlService.class);
private static final String JMS_QUEUE_PREFIX = "jms.queue.";

public List<QueueInfo> getQueues() {
Expand All @@ -37,7 +34,7 @@ public QueueInfo getQueue(String queueName) {
private JMSQueueControl getJMSQueueControl(String queueName) {
try {
ObjectName objectName = ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName);
return MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnectionFactoryBean.getObject(), objectName, JMSQueueControl.class, false);
return MBeanServerInvocationHandler.newProxyInstance(jmxConnectionClient.get(), objectName, JMSQueueControl.class, false);
} catch (Exception e) {
throw new IllegalArgumentException("Object name " + queueName + " not found!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,25 @@

import ch.filecloud.queuemonitor.common.QmonEnvironment;
import ch.filecloud.queuemonitor.common.QmonEnvironmentConfiguration;
import org.springframework.jmx.support.MBeanServerConnectionFactoryBean;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.net.MalformedURLException;
import java.util.List;

/**
* Created by domi on 10/5/14.
* @author [email protected]
*/
@Component
public class SystemInformationService {

@Inject
private QmonEnvironmentConfiguration qmonEnvironmentConfiguration;

@Inject
protected MBeanServerConnectionFactoryBean mBeanServerConnectionFactoryBean;

public List<QmonEnvironment> getEnvironments() {
return qmonEnvironmentConfiguration.getAll();
}

public void setCurrentEnvironment(String environmentName) {
QmonEnvironment qmonEnvironment = qmonEnvironmentConfiguration.get(environmentName);
try {
mBeanServerConnectionFactoryBean.setServiceUrl(qmonEnvironment.getJmxRemoteUrl());
} catch (MalformedURLException e) {
e.printStackTrace();
}
qmonEnvironmentConfiguration.setCurrent(environmentName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private List<SubscriptionInfo> getSubscriptionInfo(String topicName, TopicContro
private TopicControl getTopicControl(String topicName) {
try {
ObjectName objectName = ObjectNameBuilder.DEFAULT.getJMSTopicObjectName(topicName);
return MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnectionFactoryBean.getObject(), objectName, TopicControl.class, false);
return MBeanServerInvocationHandler.newProxyInstance(jmxConnectionClient.get(), objectName, TopicControl.class, false);
} catch (Exception e) {
throw new IllegalArgumentException("Object name not found!", e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</div>
<div ng-controller="MainController" ng-init="getEnvironmenetInfo()">
<p class="navbar-text navbar-right">
<select class="nya-selectpicker" ng-model="defaultEnv" ng-options="env.label group by env.stage for env in environments"></select>
<select class="nya-selectpicker" ng-model="defaultEnv" ng-change="updateEnvironment()" ng-options="env.label group by env.stage for env in environments"></select>
</p>
</div>
</div>
Expand Down
20 changes: 19 additions & 1 deletion src/main/webapp/scripts/controllers/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

angular.module('hornetqMonitorApp')
.controller('MainController', function ($scope, $http) {
.controller('MainController', function ($scope, $http, $route) {

$scope.defaultEnv = '';

$scope.getEnvironmenetInfo = function () {
$http.get('/monitor/system/environments').success(function(data, status, headers, config) {
Expand All @@ -12,4 +14,20 @@ angular.module('hornetqMonitorApp')
});
};

$scope.updateEnvironment = function () {
console.log('changing environment to ' + $scope.defaultEnv.name);

$scope.env = {
environmentName: $scope.defaultEnv.name
};

$http.put('/monitor/system/environments', $scope.env).success(function(data, status, headers, config) {
console.log('Successfully updated the environment to ' + $scope.defaultEnv.name);
$route.reload();
}).error(function(data, status, headers, config) {
console.log('An error occured while trying to update the environment');
});

};

});

0 comments on commit 365636b

Please sign in to comment.