简体   繁体   English

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

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

MqConnection.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 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 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);
    }
}

Here i am Using Android Studio code with backend java code to connect rabbitmq.这里我使用带有后端java代码的Android Studio代码来连接rabbitmq。 How to call message parameter in MqConsume.java from MainActivity.java如何从MainActivity.java调用MqConsume.java中的消息参数

I need to print message parameter inside MainActivity class from MqConsume.java .我需要在MqConsume.javaMainActivity 类中打印消息参数

I have tried Calling MqConsume.java inside MainActivity.java and print message parameter inside MainActivity.java .我尝试在MainActivity.java中调用MqConsume.java并在MainActivity.java中打印消息参数。

Is there any way to get data from one class to other class?有没有办法将数据从一个类获取到另一个类?

It looks like you want to retrieve data from an asynchronous API and use it in your activity.看起来您想从异步 API 中检索数据并在您的活动中使用它。 You can do this by declaring a custom interface and passing an instance of it to the function making the API call.您可以通过声明自定义接口并将其实例传递给进行 API 调用的函数来实现此目的。 Have a look here for more details about why this is needed. 在这里查看有关为什么需要这样做的更多详细信息。

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 -> { });
    }
}

Then if you call it from MainActivity you can pass in a callback like this:然后,如果您从 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.

相关问题 Java:通过反射调用第二类的方法 - Java: Method call of a second class via reflection 如何调用从不同类(第一类)运行的不同方法(用第二类编写)? - How to call a different method(written in second class) run from different class(first class)? java - 如何在一个类中的方法内进行循环以调用另一个类中的另一种方法n Java? - How do I make a loop inside a method in one class to call on another method in another class n Java? “找不到符号”-具有main方法的类可以从其他一个类中调用方法,而不能从其他另一个中调用方法? - “Cannot find symbol” - class with the main method can call methods from one of the other classes but not the second of the others? 内部类的Java方法调用与外部类的输出不同 - Java method call from inside class different output than outside class 如何在Java中从一个类调用方法到另一个 - How do i call a method in Java from one class to another java中如何在没有main方法的情况下从一个类调用toString()方法到另一个类 - How to call toString() Method from one class to another class without main method in java java无法调用第二类 - java not able to call a second class 如何调用另一个类内部的类的方法? - How to call a method of a class that is inside another class? 从Java中的方法调用类 - Call class from method in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM