Package ch02.chat; import java. io. serializable; import javax. jms. connection; import javax. jms. connectionFactory; import javax. jms. destination; import javax. jms. JMSException; import javax. jms. message; import javax. jms. messageConsumer; import javax. jms. messageListener; import javax. jms. messageProducer; import javax. jms. objectMessage; import javax. jms. queue; import javax. jms. queueSession; import javax. jms. session; /* This tool encapsulates */import javax. jms. textMessage; import javax. jms. topic; import javax. jms. topicConnection; import javax. jms. topicConnectionFactory; import javax. jms. topicPublisher; import javax. jms. topicSession; import javax. jms. topicSubscriber; import org. apache. activemq. activeMQConnection; import org. apache. activemq. activeMQConnectionFactory; public class JMSQueue {TopicConnectionFactory connectionFactory; // Co Nnection: the connection from the JMS client to the JMS Provider TopicConnection connection = null; // the session to be published // TopicSession proSession = null; // 2 subscribe session // TopicSession conSession = null; session session = null; // The topic publisher MessageProducer producer = null; // The topic MessageConsumer consumer = null; // Destination: the message Destination; to whom the message is sent. destination destination = null; // MessageProducer: message sender // Default constructor, which is connected to activemq by default. Multiple constructor public JMSQueue () {connec TionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, "tcp: // localhost: 61616"); try {connection = connectionFactory. createTopicConnection ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();} try {connection. start ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();}} Public JMSQueue (String user, String name) {connectionFactory = new ActiveMQConnectionFactory (user, name, "tcp: // localhost: 61616"); try {connection = connectionFactory. createTopicConnection ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();} try {connection. start ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace () ;}// design sessi On Type public void setSession () throws JMSException {session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE);} // Set it to the atomic type public void setAtomicSession () throws JMSException {session = connection. createSession (true, Session. AUTO_ACKNOWLEDGE);} // The fixed message here is the String type public void writeMessage (String t, String message, int priority) {try {producer = session. createProducer (session. createQueue (t )) ; // Use message to construct TextMessage text = session. createTextMessage (); text. setJMSPriority (priority); text. setText (message); producer. send (text);} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();} // you can configure the launch statement. Here, the public void writeMessage (String t, Object o) {try {producer = session is fixed. createProducer (session. createQueue (t); // use message to construct TextMessage ObjectMe Ssage text = session. createObjectMessage (); text. setObject (Serializable) o); producer. send (text);} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();} // you can configure the statement to create a press conference. Fixed It Here} // use a Message listener to listen to a Topic public void receiveMsg (String c, MessageListener ml) {try {Queue t = session. createQueue (c); consumer = session. createConsumer (t); // Set the monitor consumer. setMessageListener (Ml);} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public Message receiveMsg (String c) {try {consumer = session. createConsumer (session. createQueue (c); // Set the monitor Message = consumer. receive (); return message;} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace ();} return null;} // synchronous receiving information public void commit () throws JMSExc Eption {session. commit ();} public void rollback () throws JMSException {session. rollback ();} public void close () throws JMSException {if (connection! = Null) connection. close (); if (session! = Null) session. close (); if (producer! = Null) session. close (); if (consumer! = Null) consumer. close ();}}
Activemq p2p