Author: cmueller
Date: Thu Apr 21 21:54:58 2011
New Revision: 1095826

URL: http://svn.apache.org/viewvc?rev=1095826&view=rev
Log:
CAMEL-3853: SMPP connector lazySessionCreation

Added:
    
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppProducerLazySessionCreationTest.java
Modified:
    
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConfiguration.java
    
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppProducer.java
    
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppConfigurationTest.java

Modified: 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConfiguration.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConfiguration.java?rev=1095826&r1=1095825&r2=1095826&view=diff
==============================================================================
--- 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConfiguration.java
 (original)
+++ 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConfiguration.java
 Thu Apr 21 21:54:58 2011
@@ -57,6 +57,7 @@ public class SmppConfiguration implement
     private boolean usingSSL;
     private long initialReconnectDelay = 5000;
     private long reconnectDelay = 5000;
+    private boolean lazySessionCreation;
 
     /**
      * A POJO which contains all necessary configuration parameters for the 
SMPP connection
@@ -279,6 +280,14 @@ public class SmppConfiguration implement
     public void setReconnectDelay(long reconnectDelay) {
         this.reconnectDelay = reconnectDelay;
     }
+    
+    public boolean isLazySessionCreation() {
+        return lazySessionCreation;
+    }
+
+    public void setLazySessionCreation(boolean lazySessionCreation) {
+        this.lazySessionCreation = lazySessionCreation;
+    }
 
     @Override
     public String toString() {
@@ -307,6 +316,7 @@ public class SmppConfiguration implement
             + ", numberingPlanIndicator=" + numberingPlanIndicator
             + ", initialReconnectDelay=" + initialReconnectDelay
             + ", reconnectDelay=" + reconnectDelay
+            + ", lazySessionCreation=" + lazySessionCreation
             + "]";
     }
 }

Modified: 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppProducer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppProducer.java?rev=1095826&r1=1095825&r2=1095826&view=diff
==============================================================================
--- 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppProducer.java
 (original)
+++ 
camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppProducer.java
 Thu Apr 21 21:54:58 2011
@@ -44,8 +44,6 @@ import org.slf4j.LoggerFactory;
 /**
  * An implementation of @{link Producer} which use the SMPP protocol
  * 
- * @version 
- * @author muellerc
  */
 public class SmppProducer extends DefaultProducer {
 
@@ -54,7 +52,7 @@ public class SmppProducer extends Defaul
     private SmppConfiguration configuration;
     private SMPPSession session;
     private SessionStateListener sessionStateListener;
-    private final ReentrantLock reconnectLock = new ReentrantLock();
+    private final ReentrantLock connectLock = new ReentrantLock();
 
     public SmppProducer(SmppEndpoint endpoint, SmppConfiguration config) {
         super(endpoint);
@@ -72,15 +70,22 @@ public class SmppProducer extends Defaul
 
     @Override
     protected void doStart() throws Exception {
-        LOG.debug("Connecting to: " + getEndpoint().getConnectionString() + 
"...");
-
         super.doStart();
-        session = createSession();
-
-        LOG.info("Connected to: " + getEndpoint().getConnectionString());
+        
+        if (!getConfiguration().isLazySessionCreation()) {
+            if (connectLock.tryLock()) {
+                try {
+                    session = createSession();
+                } finally {
+                    connectLock.unlock();
+                }
+            }
+        }
     }
     
     private SMPPSession createSession() throws IOException {
+        LOG.debug("Connecting to: " + getEndpoint().getConnectionString() + 
"...");
+        
         SMPPSession session = createSMPPSession();
         session.setEnquireLinkTimer(this.configuration.getEnquireLinkTimer());
         session.setTransactionTimer(this.configuration.getTransactionTimer());
@@ -97,6 +102,8 @@ public class SmppProducer extends Defaul
                         
NumberingPlanIndicator.valueOf(configuration.getNumberingPlanIndicator()),
                         ""));
         
+        LOG.info("Connected to: " + getEndpoint().getConnectionString());
+        
         return session;
     }
     
@@ -115,6 +122,20 @@ public class SmppProducer extends Defaul
     }
 
     public void process(Exchange exchange) throws Exception {
+        if (session == null) {
+            if (getConfiguration().isLazySessionCreation()) {
+                if (connectLock.tryLock()) {
+                    try {
+                        if (session == null) {
+                            session = createSession();
+                        }
+                    } finally {
+                        connectLock.unlock();
+                    }
+                }
+            }
+        }
+        
         if (LOG.isDebugEnabled()) {
             LOG.debug("Sending a short message for exchange id '"
                     + exchange.getExchangeId() + "'...");
@@ -193,7 +214,7 @@ public class SmppProducer extends Defaul
     }
 
     private void reconnect(final long initialReconnectDelay) {
-        if (reconnectLock.tryLock()) {
+        if (connectLock.tryLock()) {
             try {
                 Runnable r = new Runnable() {
                     public void run() {
@@ -233,7 +254,7 @@ public class SmppProducer extends Defaul
             } catch (InterruptedException e) {
                 // noop
             }  finally {
-                reconnectLock.unlock();
+                connectLock.unlock();
             }
         }
     }

Modified: 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppConfigurationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppConfigurationTest.java?rev=1095826&r1=1095825&r2=1095826&view=diff
==============================================================================
--- 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppConfigurationTest.java
 (original)
+++ 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppConfigurationTest.java
 Thu Apr 21 21:54:58 2011
@@ -168,7 +168,8 @@ public class SmppConfigurationTest {
                 + "typeOfNumber=0, "
                 + "numberingPlanIndicator=0, "
                 + "initialReconnectDelay=5000, "
-                + "reconnectDelay=5000]";
+                + "reconnectDelay=5000, "
+                + "lazySessionCreation=false]";
         assertEquals(expected, configuration.toString());
     }
 

Added: 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppProducerLazySessionCreationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppProducerLazySessionCreationTest.java?rev=1095826&view=auto
==============================================================================
--- 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppProducerLazySessionCreationTest.java
 (added)
+++ 
camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/SmppProducerLazySessionCreationTest.java
 Thu Apr 21 21:54:58 2011
@@ -0,0 +1,166 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.smpp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.jsmpp.bean.Alphabet;
+import org.jsmpp.bean.BindType;
+import org.jsmpp.bean.ESMClass;
+import org.jsmpp.bean.GeneralDataCoding;
+import org.jsmpp.bean.MessageClass;
+import org.jsmpp.bean.NumberingPlanIndicator;
+import org.jsmpp.bean.RegisteredDelivery;
+import org.jsmpp.bean.SMSCDeliveryReceipt;
+import org.jsmpp.bean.SubmitSm;
+import org.jsmpp.bean.TypeOfNumber;
+import org.jsmpp.session.BindParameter;
+import org.jsmpp.session.SMPPSession;
+import org.jsmpp.session.SessionStateListener;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.aryEq;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.isA;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+/**
+ * JUnit test class for 
<code>org.apache.camel.component.smpp.SmppProducer</code>
+ * 
+ * @version 
+ * @author muellerc
+ */
+public class SmppProducerLazySessionCreationTest {
+    
+    private SmppProducer producer;
+    private SmppConfiguration configuration;
+    private SmppEndpoint endpoint;
+    private SMPPSession session;
+
+    @Before
+    public void setUp() {
+        configuration = new SmppConfiguration();
+        configuration.setLazySessionCreation(true);
+        endpoint = createMock(SmppEndpoint.class);
+        session = createMock(SMPPSession.class);
+        
+        producer = new SmppProducer(endpoint, configuration) {
+            SMPPSession createSMPPSession() {
+                return session;
+            }
+        };
+    }
+
+    private void submitSmExpectations(Exchange exchange, SmppBinding binding, 
SubmitSm submitSm) throws Exception {
+        expect(submitSm.getServiceType()).andReturn("CMT");
+        
expect(submitSm.getSourceAddrTon()).andReturn(TypeOfNumber.UNKNOWN.value());
+        
expect(submitSm.getSourceAddrNpi()).andReturn(NumberingPlanIndicator.UNKNOWN.value());
+        expect(submitSm.getSourceAddr()).andReturn("1616");
+        
expect(submitSm.getDestAddrTon()).andReturn(TypeOfNumber.UNKNOWN.value());
+        
expect(submitSm.getDestAddrNpi()).andReturn(NumberingPlanIndicator.UNKNOWN.value());
+        expect(submitSm.getDestAddress()).andReturn("1717");
+        expect(submitSm.getProtocolId()).andReturn((byte) 0);
+        expect(submitSm.getPriorityFlag()).andReturn((byte) 1);
+        
expect(submitSm.getScheduleDeliveryTime()).andReturn("090830230627004+");
+        expect(submitSm.getValidityPeriod()).andReturn("090831232000004+");
+        expect(submitSm.getRegisteredDelivery())
+            .andReturn(SMSCDeliveryReceipt.SUCCESS_FAILURE.value());
+        expect(submitSm.getReplaceIfPresent()).andReturn((byte) 0);
+        expect(submitSm.getDataCoding()).andReturn((byte) 0);
+        expect(submitSm.getShortMessage()).andReturn("Hello SMPP 
world!".getBytes("ISO-8859-1"));
+    }
+
+    @Test
+    public void doStartShouldNotCreateTheSmppSession() throws Exception {
+        
expect(endpoint.getConnectionString()).andReturn("smpp://smppclient@localhost:2775");
+    
+        replay(endpoint, session);
+    
+        producer.doStart();
+    
+        verify(endpoint, session);
+    }
+    
+    @Test
+    public void processShouldCreateTheSmppSession() throws Exception {
+        expect(endpoint.getConnectionString())
+            .andReturn("smpp://smppclient@localhost:2775")
+            .times(2);
+        session.setEnquireLinkTimer(5000); //expectation
+        session.setTransactionTimer(10000); //expectation
+        session.addSessionStateListener(isA(SessionStateListener.class));
+        expect(session.connectAndBind(
+            "localhost",
+            new Integer(2775),
+            new BindParameter(
+                    BindType.BIND_TX,
+                    "smppclient",
+                    "password",
+                    "cp",
+                    TypeOfNumber.UNKNOWN,
+                    NumberingPlanIndicator.UNKNOWN,
+                    ""))).andReturn("1");
+        
expect(endpoint.getConnectionString()).andReturn("smpp://smppclient@localhost:2775");
+        SmppBinding binding = createMock(SmppBinding.class);
+        Exchange exchange = createMock(Exchange.class);
+        Message message = createMock(Message.class);
+        SubmitSm submitSm = createMock(SubmitSm.class);
+        
expect(exchange.getExchangeId()).andReturn("ID-muellerc-macbookpro/3690-1214458315718/2-0");
+        expect(endpoint.getBinding()).andReturn(binding);
+        expect(binding.createSubmitSm(exchange)).andReturn(submitSm);
+        submitSmExpectations(exchange, binding, submitSm);
+        expect(session.submitShortMessage(
+                eq("CMT"),
+                eq(TypeOfNumber.UNKNOWN),
+                eq(NumberingPlanIndicator.UNKNOWN),
+                eq("1616"),
+                eq(TypeOfNumber.UNKNOWN),
+                eq(NumberingPlanIndicator.UNKNOWN),
+                eq("1717"),
+                isA(ESMClass.class),
+                eq((byte) 0),
+                eq((byte) 1),
+                eq("090830230627004+"),
+                eq("090831232000004+"),
+                eq(new 
RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE)),
+                eq((byte) 0),
+                eq(new GeneralDataCoding(
+                        false,
+                        true,
+                        MessageClass.CLASS1,
+                        Alphabet.ALPHA_DEFAULT)),
+                eq((byte) 0),
+                aryEq("Hello SMPP world!".getBytes("ISO-8859-1"))))
+            .andReturn("1");
+        expect(exchange.getPattern()).andReturn(ExchangePattern.InOnly);
+        expect(exchange.getIn()).andReturn(message);
+        message.setHeader(SmppBinding.ID, "1"); // expectation without return 
value
+        
expect(exchange.getExchangeId()).andReturn("ID-muellerc-macbookpro/3690-1214458315718/2-0");
+        
+        replay(session, endpoint, binding, exchange, message, submitSm);
+        
+        producer.doStart();
+        producer.process(exchange);
+        
+        verify(session, endpoint, binding, exchange, message, submitSm);
+    }
+}
\ No newline at end of file


Reply via email to