0

How to deploy CXF SOAP web service on tomcat instead of jetty server if i try to use tomcat port to run ServerFactoryBean it is showing an error:

port already running.

Is there any way to use tomcat instead of in-build jetty server?

Following is my code that i am trying to create a Server.

SoapBindingFactory bindingFactory = new SoapBindingFactory();
Bus bus = BusFactory.newInstance().createBus();
bindingFactory.setBus(bus);
bus.getExtension(BindingFactoryManager.class).registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/", bindingFactory);
bus.getExtension(BindingFactoryManager.class).registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/http", bindingFactory);
Service service = new WSDLServiceFactory(bus, "wsdl path here", null).create();

ServerFactoryBean serverFactory = new ServerFactoryBean();
serverFactory.setBus(bus);

InboundRMHttpInvoker invoker = new InboundRMHttpInvoker(serviceImpl);
serverFactory.setInvoker(invoker);

serverFactory.setServiceBean(serviceImpl);
serverFactory.setDataBinding(service.getDataBinding());
serverFactory.setServiceName(service.getName());
serverFactory.setBindingId(service.getServiceInfos().get(0).getBindings().iterator().next().getBindingId());
serverFactory.setWsdlLocation("wsdl path");
serverFactory.setEndpointName(service.getServiceInfos().iterator().next().getEndpoints().iterator().next().getName());
serverFactory.setAddress("http://localhost:8080/services/sampleservice");
Server server = serverFactory.create();

If I use another port (other than tomcat) then it deploy my service on that port but how to run it on tomcat port.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41

1 Answers1

1

You can consider developing your application with Spring Boot or similar. When you deploy the war in the application container (eg tomcat), the ports configured in the container will be used.

Update

  1. Configure a main class.
@SpringBootApplication
public class AwesomeApp extends SpringBootServletInitializer {

    public static void main(String[] args) {
        new SpringApplicationBuilder(AwesomeApp.class)
                .run(args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(AwesomeApp.class);
    }
}
  1. Configure a CXF Servlet in @Configuration class.
@Configuration
public class ServletConfig implements WebMvcConfigurer {

    public ServletRegistrationBean<CXFServlet> cxfServletRegistration() {
        ServletRegistrationBean<CXFServlet> bean = new ServletRegistrationBean<>(
                new CXFServlet(), "/services/*");
        bean.setLoadOnStartup(1);
        return bean;
    }    
}
  1. Implements the service.

  2. Configure the Endpoint in @Configuration class and register the implementation.

@Configuration
public class IntegrationConfig {
    
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    } 
    
    @Bean(name = "awesomeServiceImpl")
    public AwesomeServiceImpl awesomeServiceImpl()  {
        return new AwesomeServiceImpl();
    }
    
    @Bean
    public Endpoint awesomeEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), awesomeServiceImpl());
        endpoint.publish("/awesomeService");
        
        return endpoint;
    }   
}
  1. Run your app and browse to /services/awesomeService.
  • yes, i am using spring boot and deploying the war file in tomcat – Ganesh Gudghe Aug 19 '20 at 04:25
  • one more thing in my application user can deploy there own wsdl file , and that wsdl file i am passing to the CXF ( SoapBindingFactory ) and CXF giving me the endpoint for that WSDL – Ganesh Gudghe Aug 19 '20 at 09:39
  • In runtime?. Maybe you can register the Bean dynamically accessing the `applicationContext` and and registering it there. Something like this https://stackoverflow.com/questions/43051394/how-to-add-bean-instance-at-runtime-in-spring-webapplicationcontext – Alberto Pérez Aug 19 '20 at 10:01
  • runtime means we have web application from there user can upload there WSDL file – Ganesh Gudghe Aug 19 '20 at 10:03
  • Sorry..., I think I'm not understanding you. The user uploads the wsdl there and then what happens?. Where is or where do you load the implementation of the service? – Alberto Pérez Aug 19 '20 at 10:40
  • ok, when i upload the WSDL in CXF then CXF server is creating service for that WSDL, and i have Request and Response Interceptor ( AbstractPhaseInterceptor ) for the service logic – Ganesh Gudghe Aug 19 '20 at 11:14