【问题标题】:msf4j Runnable/Threadmsf4j 可运行/线程
【发布时间】:2018-02-21 10:14:18
【问题描述】:

我尝试让我的 msf4j jar 变得可运行(公共类 abcService 实现可运行、微服务) 当我将 jar 部署到 wso2 msf4j 容器时,可运行部分无法作为“主线程”工作,并且“子线程”未显示在控制台上。

package test.msf4j.abc;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.wso2.msf4j.Microservice;

@Path("/abcService")
public class abcService implements Runnable, Microservice {

@GET
@Path("/greeting")
public String message() {
    System.out.println("hello");
    return "Hello World";
}

public void run() {
    for (int i = 0; i < 3; i++) {
        System.out.println("Child Thread ");
        try {
            Thread.sleep(200);
        } catch (InterruptedException ie) {
            System.out.println("Child thread interrupted! " + ie);
        }
    }
    System.out.println("Child thread finished!");
}

public static void main(String[] args) {
    Thread t = new Thread(new abcService ());
    t.start();
    for (int i = 0; i < 3; i++) {
        System.out.println("Main thread ");
        try {
            Thread.sleep(200);
        } catch (InterruptedException ie) {
            System.out.println("Child thread interrupted! " + ie);
        }
    }
    System.out.println("Main thread finished!");
}
}

【问题讨论】:

  • 分享完整的sn-p
  • @YatiSawhney 更新
  • 我相信你没有调用 main 因此语句没有被打印在 restful 资源或服务层中有一个 main 方法有什么意义?
  • wso2 msf4j 容器是什么意思?为什么要在这里实现可运行?我相信您想将问候服务作为服务器运行,对吗?如果是这样,只需创建一个 MicroservuceRunner 并将您的服务实例传递给它并运行它。你不必实现 Runnable
  • 是的,有 MicroserviceRunner 的示例吗?

标签: java msf4j wso2msf4j


【解决方案1】:

我相信您想将您的问候服务作为服务器运行,对吧?如果是这样,只需创建一个 MicroservuceRunner 并将您的服务实例传递给它并运行它。你不必执行Runnable 参考MSF4J repository中的helloworld示例

基本上应该是这样的

new MicroservuceRunner().deploy(new abcService()).start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2023-03-05
    • 2019-12-29
    • 2012-12-26
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多