此问题已经在这里有了答案:
这是我的用于将消息发送到队列的代码。但是当我使用if else语句时,我的某些变量(代理,队列,用户等)被隐藏了。当我运行程序时,抛出此错误-Error:(71,13)java:找不到符号symbol:变量计数位置:class Producer
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import java.util.Scanner;
import javax.jms.*;
public class Producer {
public static void main(String[] args) throws JMSException {
Scanner scanner = new Scanner(System.in);
System.out.println("Use existing parameters/ YES or NO");
String existing = scanner.nextLine();
if (existing.equals ("NO")) {
System.out.println("Give me your broker ULR");
String broker = scanner.nextLine();
System.out.println("Give me your broker User Name");
String user = scanner.nextLine();
}else{
String broker = "failover:(ssl://b-5dfe451d-09e7-46aa-8be0-814bb4b26227-1.mq.eu-central-1.amazonaws.com:61617," +
"ssl://b-5dfe451d-09e7-46aa-8be0-814bb4b26227-2.mq.eu-central-1.amazonaws.com:61617)";
String user = "koco";
String pass = "qwertyui1234";
String queue = "queue1";
String message = "Kak si ti dobre li si kaji";
int count = 1;
}
ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory(broker);
// Specify the master username and password.
connFactory.setUserName(user);
connFactory.setPassword(pass);
//PooledConnectionFactory pooledConnFactory = new PooledConnectionFactory();
//pooledConnFactory.setConnectionFactory(connFactory);
//pooledConnFactory.setMaxConnections(10);
//Connection producerConnection = pooledConnFactory.createConnection();
Connection producerConnection = connFactory.createConnection();
producerConnection.start();
Session producerSession =
producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination producerDest = producerSession.createQueue(queue);
MessageProducer producer = producerSession.createProducer(producerDest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage producerMessage = producerSession.createTextMessage(message);
for (int i = 0; i < count; i++) {
producer.send(producerMessage);
}
if (count == 1){
System.out.println("Message sent successfully");
}else {
System.out.println("Messages sent successfully");
}
producer.close();
producerSession.close();
producerConnection.close();
}
}
您在{}块内声明了变量,所以一旦离开该块,变量就消失了。
在{}块之前声明变量,并且在块关闭之后它们仍然存在。
您需要在else语句之外定义变量计数,否则,您将无法访问它。
Scanner scanner = new Scanner(System.in);
System.out.println("Use existing parameters/ YES or NO");
String existing = scanner.nextLine();
int count = -1;
if (existing.equals ("NO")) {
System.out.println("Give me your broker ULR");
String broker = scanner.nextLine();
System.out.println("Give me your broker User Name");
String user = scanner.nextLine();
}else{
String broker = "failover:(ssl://b-5dfe451d-09e7-46aa-8be0-814bb4b26227-1.mq.eu-central-1.amazonaws.com:61617," +
"ssl://b-5dfe451d-09e7-46aa-8be0-814bb4b26227-2.mq.eu-central-1.amazonaws.com:61617)";
String user = "koco";
String pass = "qwertyui1234";
String queue = "queue1";
String message = "Kak si ti dobre li si kaji";
count = 1;
}
变量user,broker,pass,queue,count等在if else块中声明,并且是局部变量,在这些块之外不可见,您需要在类级别/全局或在方法级别至少声明这些变量在这里是Main的情况下,以便它们可以被该方法中的其他块访问。您可以在声明了String Existing
之后立即声明这些变量,但我建议不要像现在在else块中那样重新声明它们,否则这些变量将被视为特定于该特定块的局部变量,因此不会在外面可用。