Skip to content

Instantly share code, notes, and snippets.

@martint
Last active August 29, 2015 14:06
Show Gist options
  • Save martint/c44be64df882409bd3f9 to your computer and use it in GitHub Desktop.
Save martint/c44be64df882409bd3f9 to your computer and use it in GitHub Desktop.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.airlift.demo.jmx5;
import com.facebook.nifty.core.NettyServerConfig;
import com.facebook.nifty.core.NettyServerTransport;
import com.facebook.nifty.core.ThriftServerDefBuilder;
import com.facebook.nifty.processor.NiftyProcessor;
import com.facebook.swift.codec.ThriftCodecManager;
import com.facebook.swift.fb303.EmptyOptionsSource;
import com.facebook.swift.fb303.Fb303;
import com.facebook.swift.fb303.Fb303Service;
import com.facebook.swift.fb303.Fb303Source;
import com.facebook.swift.fb303.MBeanSourceAdapter;
import com.facebook.swift.fb303.ServiceInfo;
import com.facebook.swift.service.ThriftEventHandler;
import com.facebook.swift.service.ThriftServiceProcessor;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Provider;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import java.lang.management.ManagementFactory;
public class Main
{
public static void main(String[] args)
throws InterruptedException
{
// Source for built-in MBeans
MBeanSourceAdapter source = new MBeanSourceAdapter(ImmutableList.of(
"java.lang:type=Compilation",
"java.lang:type=GarbageCollector,*",
"java.lang:type=Memory",
"java.lang:type=MemoryPool,*",
"java.lang:type=OperatingSystem",
"java.lang:type=Runtime",
"java.lang:type=Threading"));
source.setMBeanServer(ManagementFactory.getPlatformMBeanServer());
// set up the fb303 service (using swift & nifty)
ThriftCodecManager codecManager = new ThriftCodecManager();
Fb303Service fb303 = new Fb303Service(codecManager,
new ServiceInfo("myservice", "1.0"),
new EmptyOptionsSource(),
ImmutableSet.<Fb303Source>of(source));
fb303.setStatusProvider(new Provider<Fb303.Status>() {
@Override
public Fb303.Status get()
{
return Fb303.Status.ALIVE;
}
});
NiftyProcessor processor = new ThriftServiceProcessor(codecManager, ImmutableList.<ThriftEventHandler>of(), fb303);
final NettyServerTransport server = new NettyServerTransport(
new ThriftServerDefBuilder()
.listen(9999)
.withProcessor(processor).build(),
NettyServerConfig.newBuilder().build(),
new DefaultChannelGroup());
server.start();
System.out.println("STARTED");
Thread.currentThread().join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment