繁体   English   中英

如何从一个类调用第二类方法并在java中的第二类中打印方法的输出

[英]How to call second class method from one class and print output of method inside second class in java

MQConnection.java

package com.example.rabbitmq;

import android.os.StrictMode;
import android.util.Log;

import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

final static String QUEUE_NAME = "hello2";
final static String EXCHANGE_NAME = "logs_monitor";

final static String HOST = "localhost";
final static int PORT = 5672;
final static Connection connection;
final static Channel channel;

public class MqConnection {
    public void setupConnection() {
        Log.d("Debug","SetupConnection connected");
        ConnectionFactory factory = new ConnectionFactory();
        try {
            factory.setHost(HOST);
            factory.setPort(PORT);
//            factory.setVirtualHost("/");
//            factory.setUsername(USERNAME);
//            factory.setPassword(PASSWORD);

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            connection = factory.newConnection();
            channel = connection.createChannel();

        } catch (IOException | TimeoutException e) {
            throw new RuntimeException("Rabbitmq problem", e);
        }
    }
}

MQConsume.java

package com.example.rabbitmq;

import android.util.Log;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;

import static com.example.rabbitmq.setVariables.EXCHANGE_NAME;
//import static com.example.rabbitmq.setVariables.QUEUE_NAME;
import static com.example.rabbitmq.setVariables.QUEUE_NAME;
import static com.example.rabbitmq.setVariables.channel;
import com.example.rabbitmq.MainActivity.*;

public class MqConsume {
    static String message = "";
    public static void consumeMessages(String[] args) throws IOException {
        Log.d("Debug","Before rabbitmq publish");
//        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            message = new String(delivery.getBody(), "UTF-8");
            Log.d("Debug","Received");
            Log.d("Debug",message);
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}

MainActivity.java

package com.example.rabbitmq;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MqConnection mqConnection = new MqConnection();
        mqConnection.setupConnection();

        MqConsume mqConsume = new MqConsume();
        final TextView received_msg;
        received_msg = findViewById(R.id.received_messages);
        System.out.println(received_msg);
    }
}

这里我使用带有后端java代码的Android Studio代码来连接rabbitmq。 如何从MainActivity.java调用MqConsume.java中的消息参数

我需要在MqConsume.javaMainActivity 类中打印消息参数

我尝试在MainActivity.java中调用MqConsume.java并在MainActivity.java中打印消息参数。

有没有办法将数据从一个类获取到另一个类?

看起来您想从异步 API 中检索数据并在您的活动中使用它。 您可以通过声明自定义接口并将其实例传递给进行 API 调用的函数来实现此目的。 在这里查看有关为什么需要这样做的更多详细信息。

public class MqConsume {

    // declare a callback interface
    public interface GotMessage {
        void gotMessage(String msg);
    }
    
    // pass the interface in here and call it when 
    // the message is received
    public static void consumeMessages(String[] args, GotMessage callback) throws IOException {
        Log.d("Debug","Before rabbitmq publish");

        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
        
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            Log.d("Debug","Received");
            Log.d("Debug",message);
            callback.gotMessage(message);
        };
        
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}

然后,如果您从 MainActivity 调用它,您可以像这样传入回调:

final TextView received_msg = findViewById(R.id.received_messages);

// create the interface in the activity and set
// view text inside it
MqConsume.consumeMessages(args, new MqConsume.GotMessage() {
    @Override
    void gotMessage(String message) {
        received_msg.setText(message);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM