[英]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.java的MainActivity 类中打印消息参数。
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.