Forum Discussion

Harald's avatar
Harald
New Contributor
10 years ago

Stop service/SoapUI/Jetty programmatically in a java class

Hi everyone,

 

I am using SoapUI programatically in a java test. With the UI I have created mock services.

With my code I am retrieving my project and my service without any problem and am running a service without any problem, but even though i am using the methods for stopping it my method won't return ([EDIT] might be because of Jetty) .

 

[EDIT 2] I haven't worked on this issue for a few weeks now, nut it seems that what I want to achieve is to set the option "No block" which you can set in Maven programmatically without Maven. Jetty is not willing to shut down. Any idea? Maybe I need to go deeper in the setup to have more options.

 

This is the code:

 

private static MockRunListenerAdapter mockRunListenerAdapter;
	static WsdlProject wsdlProject;
	static WsdlMockService wsdlMockService;
	static WsdlMockRunner wsdlMockRunner;
	
	public static void main(String args[]){
		
		mockRunListenerAdapter = new MockRunListenerAdapter();
		
		try {
			wsdlProject = new WsdlProject("D:\\SoapUIProjects\\myProject.xml");
		} catch (IOException | SoapUIException ex) {
			System.out.println("Reading wsdl failed");
		} catch (Exception exception) {
			System.out.println("Reading wsdl failed");
		}

		wsdlMockService = wsdlProject.getMockServiceByName("myService");
		wsdlMockService.addMockRunListener(mockRunListenerAdapter);
		
		System.out.println("trying to start wsdlMockService");
		try {
			wsdlMockRunner = wsdlMockService.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("wsdlMockService started and wsdlMockRunner initialized");
		try {
			wsdlMockRunner.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("wsdlMockRunner started");
		System.out.println("trying to stop wsdlMockRunner");
		wsdlMockRunner.stop();
		System.out.println("wsdlMockRunner stopped");
		System.out.println("trying to release wsdlMockService");
		wsdlMockService.release();
		System.out.println("wsdlMockService released");	
	}

 Output when running:

 

15:01:41,943 INFO  [JettyMockEngine] Started mockService [myMockService] on port [8088] at path [/myMockService]
wsdlMockService started and wsdlMockRunner initialized
wsdlMockRunner started
trying to stop wsdlMockRunner
15:01:41,959 INFO  [JettyMockEngine] Stopped MockService [myMockService] on port [8088]
wsdlMockRunner stopped
trying to release wsdlMockService
wsdlMockService released
^CTerminate batch job (Y/N)? y

I have to stop manually using CTRL+C.

What is or are the method I should use to avoir beeing blocked somewhere?

Due to my environment development I cannot debug properly

I am running the main class with a simple batch script on windows. The SoapUI GUI is of course not running.

 

[EDIT] It seems that Jetty is blocking, I am currently investigating in this direction. Hope this will help us to get forward. :)

 

Thank you in advance,

 

Harald

2 Replies

  • Harald's avatar
    Harald
    New Contributor

    Maybe it is better if i reply to my topic after two edits. It seems that Jetty is preventing my method from returning. I have seen that there i an option called "No block" for maven to avoid this type of behaviour. Does anyone have a clue about how to use it, or have something equivalent programatically? I will investigate further but I really would appreciate your help.

    Thank you,

     

    Harald

    • rupert_anderson's avatar
      rupert_anderson
      Valued Contributor

      Hi Harald,

       

      Yes, I have use the Maven nonblock option and it does certainly work i.e. if you have a SoapUI project where you need to start the mock first and then progress to run your TestCase, then the noBlock option will allow the Maven script to start the mock and then continue running the tests and exit automatically once they are complete.

       

      Here is an example pom.xml:

       

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>soapui.cookbook.chapter5</groupId>
      <artifactId>simple-test</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>simple-test</name>
      <url>http://maven.apache.org</url>
      <dependencies>
      <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
      </dependency>
      </dependencies>
      <pluginRepositories>
      <pluginRepository>
      <id>SmartBearPluginRepository</id>
      <url>http://www.soapui.org/repository/maven2/</url>
      </pluginRepository>
      </pluginRepositories>
      <build>
      <plugins>
      <plugin>
      <groupId>com.smartbear.soapui</groupId>
      <artifactId>soapui-maven-plugin</artifactId>
      <version>5.0.0</version>
      <executions>
      <execution>
      <phase>test</phase>
      <goals>
      <goal>mock</goal>
      <goal>test</goal>
      </goals>
      <configuration>
      <projectFile>src/test/resources/SOAPMock-soapui-project.xml</projectFile>
      <noBlock>true</noBlock>
      </configuration>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>
      </project>

       

      Note the same can also be achieved using Gradle or by running SoapUI via Java etc. Have also done recipe's like this in my SoapUI Cookbook (sorry more blatant self publicity! :-)) 

       

      Hope this helps,

       

      Cheers,

      Rupert