Java 添加SubscribeOn和ObserveOn后,流中方法链接未获取触发器

Java 添加SubscribeOn和ObserveOn后,流中方法链接未获取触发器,java,rx-java3,Java,Rx Java3,我正在使用rxjava3,不太明白为什么在添加ObserveOn和SubscribeOn后,流中没有调用该方法 以下是示例java代码: package mytestapp.error; import io.reactivex.rxjava3.annotations.NonNull; import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.schedulers.Schedulers; import ja

我正在使用
rxjava3
,不太明白为什么在添加
ObserveOn
SubscribeOn
后,流中没有调用该方法

以下是示例java代码:

package mytestapp.error;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        String apiUrl = "myApiUrl";
        try {
            App app = new App();
            app.syncNow(apiUrl);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void syncNow(String apiUrl) throws JSONException, IOException {
        createOrAlterTable().observeOn(Schedulers.newThread())
                .switchMap(d -> menuTableRecords(apiUrl))
                .observeOn(Schedulers.newThread()).subscribe(res -> {
                    System.out.println(res);

                }, onError -> {
                    System.out.println(onError);
                }, () -> {
                    System.out.println("Completed!!");
                });
        ;
    }

    private @NonNull Observable<Object> createOrAlterTable()
            throws IOException, JSONException {
        // Read table from backend
        // Read last sync file
        // get user data
        return Observable.zip(readTableFromBackend(), readLastSyncFile(),
                getUserData(),
                (s1, s2, s3) -> readTableFromBackendZipperFun(s1, s2, s3)).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).map(
                d -> d);
    }

    private @NonNull Observable<String> readTableFromBackend()
            throws JSONException, IOException {
        return Observable.fromArray("testing");
    }

    private @NonNull Observable<JSONObject> readLastSyncFile()
            throws JSONException {
        return Observable.fromArray(new JSONObject());
    }

    private @NonNull Observable<Boolean> getUserData() throws JSONException {
        return Observable.fromArray(true);
    }

    private JSONArray readTableFromBackendZipperFun(String sqlliteDDL,
            JSONObject lastFV, boolean userDataFlag) throws JSONException {
        System.out.println("zip ops");
        return new JSONArray();
    }

    private @NonNull Observable<String> menuTableRecords(String apiUrl)
            throws JSONException, IOException {
        
        return Observable.fromArray("MENU_TABLE_RECORDS");
    }
}
包mytestapp.error;
导入io.reactivex.rxjava3.annotations.NonNull;
导入io.reactivex.rxjava3.core.Observable;
导入io.reactivex.rxjava3.schedulers.schedulers;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.nio.charset.charset;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入org.json.JSONTokener;
/**
*你好,世界!
*
*/
公共类应用程序{
公共静态void main(字符串[]args){
System.out.println(“你好,世界!”);
字符串apirl=“myapirl”;
试一试{
App App=新App();
app.syncNow(apirl);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有void syncNow(字符串APIRL)抛出JSONException、IOException{
createOrAlterTable().observeOn(Schedulers.newThread())
.switchMap(d->menuTableRecords(apiUrl))
.observeOn(Schedulers.newThread()).subscribe(res->{
系统输出打印项次(res);
},onError->{
系统输出打印项次(onError);
}, () -> {
System.out.println(“已完成!!”;
});
;
}
private@NonNull可观测createOrAlterTable()
抛出IOException、JSONException{
//从后端读取表
//读取上次同步文件
//获取用户数据
返回Observable.zip(readTableFromBackend(),readLastSyncFile(),
getUserData(),
(s1,s2,s3)->readTableFromBackendZipperFun(s1,s2,s3)).subscribeOn(Schedulers.io())
.observeOn(Schedulers.io()).map(
d->d);
}
private@NonNull可观察的readTableFromBackend()
抛出JSONException、IOException{
从阵列(“测试”)返回可观察的数据;
}
private@NonNull可观测readLastSyncFile()
抛出JSONException{
返回Observable.fromArray(新的JSONObject());
}
private@NonNull Observable getUserData()抛出JSONException{
从数组返回可观测值(true);
}
私有JSONArray readTableFromBackendZipperFun(字符串sqlliteDDL,
JSONObject lastFV,布尔userDataFlag)抛出JSONException{
System.out.println(“zip操作”);
返回新的JSONArray();
}
private@NonNull可观察menuTableRecords(字符串APIRL)
抛出JSONException、IOException{
返回可观察的.fromArray(“菜单表记录”);
}
}
我希望在单独的线程上执行每个方法,并在不同的线程上执行订阅

原因是什么?如何解决上述问题


谢谢。

看起来您没有等待异步活动在
main
方法中完成,应用程序就退出了。建议阅读:

在这种特殊情况下,您可以通过使用
blockingSubscribe
而不仅仅是
subscribe
来解决此问题