Error Image
I’m having this problem where I’m trying to make a message app. Everything about the messaging is working fine, but when openning multiple terminals to make conversations between the clients, eventually the line breaks will be broken and line will appear at the end of others when it should be printed at the bottom.
I’m using maven to compile, so could it be the problem?
<code>import java.util.Scanner;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Emissor {
private static String target = "";
private static String currentUser = "";
private static String message;
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("ip"); // Alterar
factory.setUsername("user"); // Alterar
factory.setPassword("password"); // Alterar
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Scanner sc = new Scanner(System.in);
System.out.print("User: ");
currentUser = sc.nextLine();
channel.queueDeclare(currentUser, false, false, false, null);
safePrintln("nLogado com sucesso!");
System.out.print("Destinatário: ");
target = sc.nextLine();
while (true) {
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String messageReceived = new String(body, "UTF-8");
safePrintln("n[x] Mensagem recebida: " + messageReceived);
safePrint(target + " >> ");
}
};
channel.basicConsume(currentUser, true, consumer);
safePrint(target + " >> ");
message = sc.nextLine();
if (message.isEmpty()) {
continue;
}
if (message.toLowerCase().equals("sair")) {
break;
}
channel.basicPublish("", target, null, message.getBytes("UTF-8"));
}
sc.close();
channel.close();
connection.close();
}
private static void safePrintln(String s) {
synchronized (System.out) {
System.out.println(s);
}
}
private static void safePrint(String s) {
synchronized (System.out) {
System.out.print(s);
}
}
}
</code>
<code>import java.util.Scanner;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Emissor {
private static String target = "";
private static String currentUser = "";
private static String message;
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("ip"); // Alterar
factory.setUsername("user"); // Alterar
factory.setPassword("password"); // Alterar
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Scanner sc = new Scanner(System.in);
System.out.print("User: ");
currentUser = sc.nextLine();
channel.queueDeclare(currentUser, false, false, false, null);
safePrintln("nLogado com sucesso!");
System.out.print("Destinatário: ");
target = sc.nextLine();
while (true) {
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String messageReceived = new String(body, "UTF-8");
safePrintln("n[x] Mensagem recebida: " + messageReceived);
safePrint(target + " >> ");
}
};
channel.basicConsume(currentUser, true, consumer);
safePrint(target + " >> ");
message = sc.nextLine();
if (message.isEmpty()) {
continue;
}
if (message.toLowerCase().equals("sair")) {
break;
}
channel.basicPublish("", target, null, message.getBytes("UTF-8"));
}
sc.close();
channel.close();
connection.close();
}
private static void safePrintln(String s) {
synchronized (System.out) {
System.out.println(s);
}
}
private static void safePrint(String s) {
synchronized (System.out) {
System.out.print(s);
}
}
}
</code>
import java.util.Scanner;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Emissor {
private static String target = "";
private static String currentUser = "";
private static String message;
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("ip"); // Alterar
factory.setUsername("user"); // Alterar
factory.setPassword("password"); // Alterar
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Scanner sc = new Scanner(System.in);
System.out.print("User: ");
currentUser = sc.nextLine();
channel.queueDeclare(currentUser, false, false, false, null);
safePrintln("nLogado com sucesso!");
System.out.print("Destinatário: ");
target = sc.nextLine();
while (true) {
Consumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String messageReceived = new String(body, "UTF-8");
safePrintln("n[x] Mensagem recebida: " + messageReceived);
safePrint(target + " >> ");
}
};
channel.basicConsume(currentUser, true, consumer);
safePrint(target + " >> ");
message = sc.nextLine();
if (message.isEmpty()) {
continue;
}
if (message.toLowerCase().equals("sair")) {
break;
}
channel.basicPublish("", target, null, message.getBytes("UTF-8"));
}
sc.close();
channel.close();
connection.close();
}
private static void safePrintln(String s) {
synchronized (System.out) {
System.out.println(s);
}
}
private static void safePrint(String s) {
synchronized (System.out) {
System.out.print(s);
}
}
}
Already tried to synchronize the threads using “synchronized (System.out)” but the results are the same.