Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
package de.limago.jms;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class MessageConsumer implements MessageListener{
// Herstellerspezifischer Treiber. Treiber muss im Classpath liegen
// Im Falle des Jboss, einfach die JbossAllClient.jar aus dem JBOSS-Client Verzeichnis verwenden
private static final String CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
// Auch JBOSS spezifisch
private static final String PROVIDER_URL ="jnp://localhost:1099";
private static final String CONNECTION_FACTORY = "ConnectionFactory";
// Name der Queue (Postfach) das ich auf dem JBOSS angelegt habe...
private static final String QUEUE ="queue/tutorials";
public MessageConsumer() throws Exception {
// Erzeugen der Receivers und registrieren des MessageConsumers...
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,CONTEXT_FACTORY );
props.put(Context.PROVIDER_URL,PROVIDER_URL );
InitialContext ic = new InitialContext(props);
QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup(CONNECTION_FACTORY);
QueueConnection queueConnection = qcf.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ic.lookup(QUEUE);
QueueReceiver receiver = queueSession.createReceiver(queue);
receiver.setMessageListener(this);
queueConnection.start();
}
public static void main(String[] args) throws Exception{
new MessageConsumer();
}
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage)
System.out.println(((TextMessage)msg).getText());
else
System.out.println("Falscher Nachrichtentyp");
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package de.limago.jms;
import java.util.Properties;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class MessageProducer {
private static final String CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
private static final String PROVIDER_URL ="jnp://localhost:1099";
private static final String CONNECTION_FACTORY = "ConnectionFactory";
private static final String QUEUE ="queue/tutorials";
public MessageProducer() throws Exception{
System.out.println("Start");
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,CONTEXT_FACTORY );
props.put(Context.PROVIDER_URL,PROVIDER_URL );
InitialContext ic = new InitialContext(props);
QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup(CONNECTION_FACTORY);
QueueConnection queueConnection = qcf.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
TextMessage textMessage = queueSession.createTextMessage();
textMessage.setText("Hallo Welt!");
//textMessage.setJMSReplyTo( (Queue) ic.lookup(QUEUE));
Queue queue = (Queue) ic.lookup(QUEUE);
QueueSender sender = queueSession.createSender(queue);
sender.send(textMessage);
System.out.println("Nachricht verschickt!");
}
public static void main(String[] args) throws Exception {
new MessageProducer();
}
}