001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Jeff Mesnil.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.scheduler;
021:
022: import java.util.HashMap;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import javax.management.NotCompliantMBeanException;
030: import javax.management.openmbean.TabularData;
031:
032: import org.continuent.sequoia.common.i18n.Translate;
033: import org.continuent.sequoia.common.jmx.management.TransactionDataSupport;
034: import org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean;
035: import org.continuent.sequoia.controller.jmx.AbstractStandardMBean;
036: import org.continuent.sequoia.controller.requestmanager.TransactionMetaData;
037: import org.continuent.sequoia.controller.requests.AbstractRequest;
038:
039: /**
040: * AbstractSchedulerControlMBean implemementation. Used to manage Schedulers
041: *
042: * @see org.continuent.sequoia.controller.scheduler.AbstractScheduler
043: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean
044: */
045: public class AbstractSchedulerControl extends AbstractStandardMBean
046: implements AbstractSchedulerControlMBean {
047: private AbstractScheduler managedScheduler;
048:
049: /**
050: * Creates a new <code>AbstractSchedulerControl</code> object
051: *
052: * @param scheduler the managed AbstractScheduler
053: * @throws NotCompliantMBeanException if this mbean is not compliant
054: */
055: public AbstractSchedulerControl(AbstractScheduler scheduler)
056: throws NotCompliantMBeanException {
057: super (AbstractSchedulerControlMBean.class);
058: this .managedScheduler = scheduler;
059: }
060:
061: /**
062: * @see org.continuent.sequoia.controller.jmx.AbstractStandardMBean#getAssociatedString()
063: */
064: public String getAssociatedString() {
065: return "abstractscheduler"; //$NON-NLS-1$
066: }
067:
068: /**
069: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#listActiveTransactionIds()
070: */
071: public long[] listActiveTransactionIds() {
072: List transactions = managedScheduler.getActiveTransactions();
073: int sz = transactions.size();
074: long[] res = new long[sz];
075: for (int i = 0; i < sz; i++) {
076: TransactionMetaData tmd = (TransactionMetaData) transactions
077: .get(i);
078: res[i] = tmd.getTransactionId();
079: }
080: return res;
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#getActiveTransactions()
087: */
088: public TabularData getActiveTransactions() throws Exception {
089: List transactions = managedScheduler.getActiveTransactions();
090: TabularData data = TransactionDataSupport.newTabularData();
091: long now = System.currentTimeMillis();
092: for (int i = 0; i < transactions.size(); i++) {
093: TransactionMetaData tmd = (TransactionMetaData) transactions
094: .get(i);
095: long time = (now - tmd.getTimestamp()) / 1000;
096: data.put(TransactionDataSupport.newCompositeData(tmd
097: .getTransactionId(), time));
098: }
099: return data;
100: }
101:
102: private long[] listPendingRequestIds(Map requests) {
103: Set reqIds = requests.keySet();
104: long[] res = new long[reqIds.size()];
105: int i = 0;
106: for (Iterator iter = reqIds.iterator(); iter.hasNext(); i++) {
107: Long l = (Long) iter.next();
108: res[i] = l.longValue();
109: }
110: return res;
111: }
112:
113: /**
114: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#listPendingWriteRequestIds()
115: */
116: public long[] listPendingWriteRequestIds() {
117: return listPendingRequestIds(managedScheduler
118: .getActiveWriteRequests());
119: }
120:
121: /**
122: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#listPendingReadRequestIds()
123: */
124: public long[] listPendingReadRequestIds() {
125: return listPendingRequestIds(managedScheduler
126: .getActiveReadRequests());
127: }
128:
129: /**
130: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#listOpenPersistentConnections()
131: */
132: public Hashtable listOpenPersistentConnections() {
133: return managedScheduler.getOpenPersistentConnections();
134: }
135:
136: /**
137: * @see org.continuent.sequoia.common.jmx.mbeans.AbstractSchedulerControlMBean#dumpRequest(long)
138: */
139: public String dumpRequest(long requestId) {
140: AbstractRequest request = null;
141: Map writeRequests = managedScheduler.getActiveWriteRequests();
142: synchronized (writeRequests) {
143: request = (AbstractRequest) writeRequests.get(new Long(
144: requestId));
145: }
146: if (request == null) {
147: Map readRequests = managedScheduler.getActiveReadRequests();
148: synchronized (readRequests) {
149: request = (AbstractRequest) readRequests.get(new Long(
150: requestId));
151: }
152: }
153: if (request == null) {
154: return Translate.get("scheduler.request.notActive",
155: requestId);
156: }
157: return request.toDebugString();
158: }
159: }
|