Results 1 to 3 of 3

Thread: Thread Safety question related to jaxb2Mashaller and SpringMVC

  1. #1
    Join Date
    Feb 2011
    Posts
    2

    Default Thread Safety question related to jaxb2Mashaller and SpringMVC

    Hi,

    I am new to developing web apps with java and Spring Framework. I have read about thread safety being an important point that needs to be considered while using java for web application development and have fairly understood the reasons behind it. However, while doing the actual coding, I am unable to figure out which beans will be Singleton and which will not.

    In my case, Jaxb2Marshaller is used for working with xml data in a REST application. A Customer class annotated as @XmlRootElement is as follows -

    Code:
    @XmlRootElement(name="customer")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer implements RequestNode {
    	
    	private List<String> validationErrors;
    	
    	@XmlElement(name="name")
    	private String name;
    	
    	@XmlElement(name="email")
    	private String email;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	
    	public Boolean validate() {
    		
    		this.validationErrors = new ArrayList<String>();
    		
    		if(this.name == null || this.name.equals("")){
    			this.validationErrors.add("101");
    		}
    		
    		if(this.email == null || this.email.equals("")){
    			this.validationErrors.add("102");
    		}
    		
    		if(this.validationErrors.isEmpty()){
    			return true;
    		}
    		
    		return false;
    	}
    	
    	@Override
    	public List<String> getValidationErrors() {
    		return this.validationErrors;
    	}
    	
    }
    And the controller action is as follows

    Code:
            @RequestMapping(method=RequestMethod.POST, value="/generate")
    	public String generate(@RequestBody String body, Model model){
    		
    		Source source = new StreamSource(new StringReader(body));
    		
    		Customer c = (Customer) this.jaxb2Mashaller.unmarshal(source);
    		
    		//TODO generate response and add response obj to model			
    		
    		return "mytemplate";
    	}
    Since the Customer class instance is being created by jaxb2Mashaller , its not clear whether an instance variable in the Customer class will lead to thread safety issues.

    So in general my question is, while using Spring, is it possible to tell in which classes instance variables can be used and where they need to be avoided ?

    Thanks
    Last edited by naiquevin; Feb 18th, 2011 at 03:44 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Use [ code][/code ] tags when posting code that way it remains readable.

    Your current setup doesn't lead to any problems a customer is simply recreate when you call unmarshal (it gives you a new Customer)...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2011
    Posts
    2

    Default

    Thanks for the reply.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •