Java tutorial
package com.cloudhopper.smpp.demo; /* * #%L * ch-smpp * %% * Copyright (C) 2009 - 2012 Cloudhopper by Twitter * %% * 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. * #L% */ import com.cloudhopper.smpp.*; import com.cloudhopper.smpp.impl.DefaultSmppServer; import com.cloudhopper.smpp.impl.DefaultSmppSessionHandler; import com.cloudhopper.smpp.pdu.BaseBind; import com.cloudhopper.smpp.pdu.BaseBindResp; import com.cloudhopper.smpp.pdu.PduRequest; import com.cloudhopper.smpp.pdu.PduResponse; import com.cloudhopper.smpp.type.SmppProcessingException; import io.netty.channel.nio.NioEventLoopGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.ref.WeakReference; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; /** * * @author joelauer (twitter: @jjlauer or <a href="http://twitter.com/jjlauer" target=window>http://twitter.com/jjlauer</a>) */ public class ServerMain { private static final Logger logger = LoggerFactory.getLogger(ServerMain.class); static public void main(String[] args) throws Exception { // // setup 3 things required for a server // // create and assign the NioEventLoopGroup instances to handle event processing, // such as accepting new connections, receiving data, writing data, and so on. NioEventLoopGroup group = new NioEventLoopGroup(); // to enable automatic expiration of requests, a second scheduled executor // is required which is what a monitor task will be executed with - this // is probably a thread pool that can be shared with between all client bootstraps ScheduledThreadPoolExecutor monitorExecutor = (ScheduledThreadPoolExecutor) Executors .newScheduledThreadPool(1, new ThreadFactory() { private AtomicInteger sequence = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("SmppServerSessionWindowMonitorPool-" + sequence.getAndIncrement()); return t; } }); // create a server configuration SmppServerConfiguration configuration = new SmppServerConfiguration(); configuration.setPort(2776); configuration.setMaxConnectionSize(10); configuration.setNonBlockingSocketsEnabled(true); configuration.setDefaultRequestExpiryTimeout(30000); configuration.setDefaultWindowMonitorInterval(15000); configuration.setDefaultWindowSize(5); configuration.setDefaultWindowWaitTimeout(configuration.getDefaultRequestExpiryTimeout()); configuration.setDefaultSessionCountersEnabled(true); configuration.setJmxEnabled(true); // create a server, start it up DefaultSmppServer smppServer = new DefaultSmppServer(configuration, new DefaultSmppServerHandler(), monitorExecutor, group, group); logger.info("Starting SMPP server..."); smppServer.start(); logger.info("SMPP server started"); System.out.println("Press any key to stop server"); System.in.read(); logger.info("Stopping SMPP server..."); smppServer.stop(); logger.info("SMPP server stopped"); logger.info("Server counters: {}", smppServer.getCounters()); } public static class DefaultSmppServerHandler implements SmppServerHandler { @Override public void sessionBindRequested(Long sessionId, SmppSessionConfiguration sessionConfiguration, final BaseBind bindRequest) throws SmppProcessingException { // test name change of sessions // this name actually shows up as thread context.... sessionConfiguration.setName("Application.SMPP." + sessionConfiguration.getSystemId()); //throw new SmppProcessingException(SmppConstants.STATUS_BINDFAIL, null); } @Override public void sessionCreated(Long sessionId, SmppServerSession session, BaseBindResp preparedBindResponse) throws SmppProcessingException { logger.info("Session created: {}", session); // need to do something it now (flag we're ready) session.serverReady(new TestSmppSessionHandler(session)); } @Override public void sessionDestroyed(Long sessionId, SmppServerSession session) { logger.info("Session destroyed: {}", session); // print out final stats if (session.hasCounters()) { logger.info(" final session rx-submitSM: {}", session.getCounters().getRxSubmitSM()); } // make sure it's really shutdown session.destroy(); } } public static class TestSmppSessionHandler extends DefaultSmppSessionHandler { private WeakReference<SmppSession> sessionRef; public TestSmppSessionHandler(SmppSession session) { this.sessionRef = new WeakReference<SmppSession>(session); } @Override public PduResponse firePduRequestReceived(PduRequest pduRequest) { SmppSession session = sessionRef.get(); // mimic how long processing could take on a slower smsc try { //Thread.sleep(50); } catch (Exception e) { } return pduRequest.createResponse(); } } }