Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:15:32+00:00 2026-06-14T06:15:32+00:00

I have a class annotated with @Path and in this class I have a

  • 0

I have a class annotated with @Path and in this class I have a method that handles a PUT request.

How can I do this?

@PUT
@Consumes(MediaType.TEXT_PLAIN)
public void putString(String myString) throws JMSException {  
   if (txtmessage != null && producer != null ){
           txtmessage.clearBody();
           txtmessage.setText(myString);
           producer.send(txtmessage);
         }    
      }

all the initialization is done

Does jersey let you handle on close events?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:15:33+00:00Added an answer on June 14, 2026 at 6:15 am

    Implement the

    ServletContextListener: http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/ServletContextListener.html

    import javax.jms.Connection;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.apache.log4j.Logger;
    
    public class MyContextListener implements ServletContextListener {
        public final static String ACTIVE_MQ_SESSION = "ActiveMQSession";
        public final static String ACTIVE_MQ_PRODUCER = "ActiveMQProducer";
    
        Logger logger = Logger.getLogger(this.getClass());
        private static final int ackMode = Session.AUTO_ACKNOWLEDGE;
        private static final boolean transacted = false;
    
        private static final String brokerUrl = "vm://localhost:61616";
    
        private Connection connection;
        private Session session;
        private MessageProducer producer;
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            try {
                this.producer.close();
                this.session.close();
                this.connection.close();
            } catch (JMSException e) {
                logger.warn("tearDown()", e);
            }
    
        }
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    brokerUrl);
    
            try {
                connection = connectionFactory.createConnection();
                connection.start();
                session = connection.createSession(transacted, ackMode);
                Destination destination = session.createQueue("queue");
                producer = session.createProducer(destination);
    
                ServletContext sc = sce.getServletContext();
                sc.setAttribute(ACTIVE_MQ_SESSION, session);
                sc.setAttribute(ACTIVE_MQ_PRODUCER, producer);
            } catch (JMSException e) {
                logger.warn("setup() failed to setup connection brokerUrl="
                        + brokerUrl);
            }
        }
    
    }
    

    Register the listener in web.xml:

    <web-app...>
        <listener>
            <listener-class>package.MyContextListener</listener-class>
        </listener>
    </web-app>
    

    and then the servlet (from where you use the producer and session):

    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.servlet.http.HttpServlet;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.PUT;
    import javax.ws.rs.core.MediaType;
    
    public class MessageServlet extends HttpServlet {
    
        @PUT
        @Consumes(MediaType.TEXT_PLAIN)
        public void putString(String myString) throws JMSException {
            MessageProducer producer = (MessageProducer) getServletContext()
                    .getAttribute(MyContextListener.ACTIVE_MQ_PRODUCER);
    
            Session session = (Session) getServletContext().getAttribute(
                    MyContextListener.ACTIVE_MQ_SESSION);
            TextMessage txtmessage = session.createTextMessage();
            if (txtmessage != null && producer != null) {
                txtmessage.clearBody();
                txtmessage.setText(myString);
                producer.send(txtmessage);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class this is annotated with @Path like so: @Path(widgets) @Produces(MediaType.APPLICATION_XML) public
Let's say that I have this class: @Singleton public class Parent { ... }
We have a @WebServlet that's annotated with a custom interceptor annotation like this: @WebServlet(/path)
I have a class annotated like this: @Entity @Table(name=MYENTITY) @SequenceGenerator(name=CODE_GEN, sequenceName=SEQ_NAME) public class MyEntity
I have a data transfer object that's annotated with JSR-303 constraints like... public class
I have a class that is annotated as the @XmlRootElement with @XmlAccessorType(XmlAccessType.NONE) . The
Lets say we have this JPA-annotated class, with a property of type List. This
I have a DateInterval class that is annotated with @Embeddable and it has two
I have a Java class annotated with @XmlRootElement . This Java class has a
I have a class A that holds a class B like this: class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.