简体   繁体   中英

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

Here i am Using Android Studio code with backend java code to connect rabbitmq. How to call message parameter in MqConsume.java from MainActivity.java

I need to print message parameter inside MainActivity class from MqConsume.java .

I have tried Calling MqConsume.java inside MainActivity.java and print message parameter inside 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. You can do this by declaring a custom interface and passing an instance of it to the function making the API call. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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