001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.broker.jmx; 018 019import java.io.IOException; 020import java.util.Set; 021 022import javax.management.ObjectName; 023 024import org.apache.activemq.broker.Connection; 025import org.apache.activemq.util.IOExceptionSupport; 026import org.apache.activemq.util.JMXSupport; 027 028public class ConnectionView implements ConnectionViewMBean { 029 030 private final Connection connection; 031 private final ManagementContext managementContext; 032 private String userName; 033 034 public ConnectionView(Connection connection) { 035 this(connection, null); 036 } 037 038 public ConnectionView(Connection connection, ManagementContext managementContext) { 039 this.connection = connection; 040 this.managementContext = managementContext; 041 } 042 043 @Override 044 public void start() throws Exception { 045 connection.start(); 046 } 047 048 @Override 049 public void stop() throws Exception { 050 connection.stop(); 051 } 052 053 /** 054 * @return true if the Connection is slow 055 */ 056 @Override 057 public boolean isSlow() { 058 return connection.isSlow(); 059 } 060 061 /** 062 * @return if after being marked, the Connection is still writing 063 */ 064 @Override 065 public boolean isBlocked() { 066 return connection.isBlocked(); 067 } 068 069 /** 070 * @return true if the Connection is connected 071 */ 072 @Override 073 public boolean isConnected() { 074 return connection.isConnected(); 075 } 076 077 /** 078 * @return true if the Connection is active 079 */ 080 @Override 081 public boolean isActive() { 082 return connection.isActive(); 083 } 084 085 @Override 086 public int getDispatchQueueSize() { 087 return connection.getDispatchQueueSize(); 088 } 089 090 /** 091 * Resets the statistics 092 */ 093 @Override 094 public void resetStatistics() { 095 connection.getStatistics().reset(); 096 } 097 098 @Override 099 public String getRemoteAddress() { 100 return connection.getRemoteAddress(); 101 } 102 103 public String getConnectionId() { 104 return connection.getConnectionId(); 105 } 106 107 @Override 108 public String getUserName() { 109 return userName; 110 } 111 112 public void setUserName(String userName) { 113 this.userName = userName; 114 } 115 116 @Override 117 public ObjectName[] getConsumers() { 118 ObjectName[] result = null; 119 120 if (connection != null && managementContext != null) { 121 122 try { 123 ObjectName query = createConsumerQueury(connection.getConnectionId()); 124 Set<ObjectName> names = managementContext.queryNames(query, null); 125 result = names.toArray(new ObjectName[0]); 126 } catch (Exception e) { 127 } 128 } 129 130 return result; 131 } 132 133 @Override 134 public ObjectName[] getProducers() { 135 ObjectName[] result = null; 136 137 if (connection != null && managementContext != null) { 138 139 try { 140 ObjectName query = createProducerQueury(connection.getConnectionId()); 141 Set<ObjectName> names = managementContext.queryNames(query, null); 142 result = names.toArray(new ObjectName[0]); 143 } catch (Exception e) { 144 } 145 } 146 147 return result; 148 } 149 150 private ObjectName createConsumerQueury(String clientId) throws IOException { 151 try { 152 return new ObjectName(managementContext.getJmxDomainName() + ":" + "BrokerName=*," 153 + "Type=Subscription,persistentMode=*," 154 + "destinationType=*,destinationName=*," 155 + "clientId=" + JMXSupport.encodeObjectNamePart(clientId) + "," 156 + "consumerId=*"); 157 } catch (Throwable e) { 158 throw IOExceptionSupport.create(e); 159 } 160 } 161 162 private ObjectName createProducerQueury(String clientId) throws IOException { 163 try { 164 return new ObjectName(managementContext.getJmxDomainName() + ":" + "BrokerName=*," 165 + "Type=Producer," 166 + "destinationType=*,destinationName=*," 167 + "clientId=" + JMXSupport.encodeObjectNamePart(clientId) + "," 168 + "producerId=*"); 169 } catch (Throwable e) { 170 throw IOExceptionSupport.create(e); 171 } 172 } 173}