alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (BusShutdownTest.java)

This example Apache CXF source code file (BusShutdownTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

assert, bindingprovider, bindingprovider, bus, busshutdowntest, greeter, net, network, port, port, reflection, soapservice, springbusfactory, springbusfactory, string, string, test, xml

The Apache CXF BusShutdownTest.java source code

/**
 * 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.cxf.systest.bus;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.URL;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.test.TestUtilities;
import org.apache.cxf.testutil.common.TestUtil;
import org.apache.hello_world_soap_http.Greeter;
import org.apache.hello_world_soap_http.GreeterImpl;
import org.apache.hello_world_soap_http.SOAPService;

import org.junit.AfterClass;
import org.junit.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

public class BusShutdownTest extends Assert {
    public static final String PORT = TestUtil.getPortNumber(BusShutdownTest.class);

    
    @BeforeClass
    public static void setKeepAliveProperty() {
        TestUtilities.setKeepAliveSystemProperty(false);
    }
    
    @AfterClass
    public static void cleanKeepAliveProperty() {
        TestUtilities.recoverKeepAliveSystemProperty();
    }
    
    
    
    @Test
    public void testStartWorkShutdownWork() throws Exception {
        
        
        URL wsdlUrl = getClass().getResource("/wsdl/hello_world.wsdl");
        assertNotNull("wsdl resource was not found", wsdlUrl); 
        // Since this test always fails in linux box, try to use other port
        String serverAddr = "http://localhost:" + PORT + "/greeter/port";
        makeTwoWayCallOnNewBus(wsdlUrl, serverAddr);
        
        
        // verify sutdown cleans the slate and reverts to null state
        //
        // This test should not need to workaroundHangWithDifferentAddr,
        // and when using ADDR, the test should not need: 
        //  org.apache.cxf.transports.http_jetty.DontClosePort
        //
        String workaroundHangWithDifferentAddr = serverAddr;
        
        // reusing same address will cause failure, hang on client invoke
        //possibleWorkaroundHandWithDifferentAddr = ADDR.replace('8', '9');
        makeTwoWayCallOnNewBus(wsdlUrl, workaroundHangWithDifferentAddr);
    }

    private void makeTwoWayCallOnNewBus(URL wsdlUrl, String address) {
        SpringBusFactory bf = new SpringBusFactory();
        Bus bus = bf.createBus();
        SpringBusFactory.setDefaultBus(bus);
        createService(address);
        doWork(wsdlUrl, address);
        // this should revert the JVM to its original state pending gc
        bus.shutdown(false);
    }

    private void doWork(URL wsdlUrl, String address) {
        SOAPService service = new SOAPService(wsdlUrl);
        assertNotNull(service);        
        Greeter greeter = service.getSoapPort();
        
        // overwrite client address
        InvocationHandler handler  = Proxy.getInvocationHandler(greeter);
        BindingProvider bp = (BindingProvider)handler;    
        bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                                   address);
        // invoke twoway call
        greeter.sayHi();
        
    }

    private void createService(String address) {        
        Object impl = new GreeterImpl();
        Endpoint.publish(address, impl);
    }
}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF BusShutdownTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.