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 */
017
018package org.apache.activemq.transport;
019
020import java.io.IOException;
021
022/**
023 * The thread name filter, modifies the name of the thread during the invocation to a transport.
024 * It appends the remote address, so that a call stuck in a transport method such as socketWrite0
025 * will have the destination information in the thread name.
026 * This is extremely useful for thread dumps when debugging.
027 * To enable this transport, in the transport URI, simpley add<br/>
028 * <code>transport.threadName</code>.<br/>
029 * For example:</br>
030 * <pre><code>
031 * &lt;transportConnector 
032 *     name=&quot;tcp1&quot; 
033 *     uri=&quot;tcp://127.0.0.1:61616?transport.soTimeout=10000&amp;transport.threadName"
034 * /&gt;
035 * </code></pre>
036 * @author Filip Hanik
037 *
038 */
039public class ThreadNameFilter extends TransportFilter {
040
041    public ThreadNameFilter(Transport next) {
042        super(next);
043    }
044
045    @Override
046    public void oneway(Object command) throws IOException {
047        String address =(next!=null?next.getRemoteAddress():null); 
048        if (address!=null) {
049            String name = Thread.currentThread().getName();
050            try {
051                String sendname = name + " - SendTo:"+address;
052                Thread.currentThread().setName(sendname);
053                super.oneway(command);
054            }finally {
055                Thread.currentThread().setName(name);
056            }
057        } else {
058            super.oneway(command);
059        }
060    }
061    
062    
063
064}