I'm building an IM program using java IO, and I have an Object called Message.
what field do you recommend me to add to Message Class?
I did the folowing:
public class Message implements Serializable {
static private final long serialVersionUID=12525452;
enum commands{
LEAVE,
ONLINELISTREQUEST,
SENT,
DELIVERED,
READ;
}
enum types{
TEXT,
VEDIO,
PICTURE,
AUDIO,
COMMAND,
//...... what to add??
}
// fields..
private String From;
private String To;
private String Body;
private int type;
private String url;
private int command;
//what to add??
ALso have variable as STATE which will be having values as:-
SEEN, SENT, etc..
This will help in tracking of message and also launch a threads which will keep on checking whether messages which don't have status as SENT/ RECEIVED just resend them
Just use the teachings of Object Oriented concepts. A class should have attributes that are really properties of the entity represented by that class.
Related
I have 3 SpringBoot projects:
Producer (producer of messages)
Consumer (consumer of messages)
Commons (Domain objects shared b/w Producer and Consumer)
Message class is as below:
public class Message {
private String eventType;
private String Payload; //serialized
}
Payload class is as below:
public class Payload {
private DomainObjectA a;
private DomainObjectB b;
private DomainObjectC c;
private DomainObjectD d;
}
There are 2 type of events CREATE and UPDATE, and the Producer application can produce 2 types of Messages(to ActiveMQ) depending on type of event.
I need to maintain a single queue in Consumer project which will accept Messages for both kind of events.
For CREATE event I need to build all domain objects, while for UPDATE event need to build DomainObjectA and DomainObjectB only.
if("CREATE".equals(eventType)
{
build DomainObjectA;
build DomainObjectB;
build DomainObjectC;
build DomainObjectD;
set above objects in Payload object
set Payload from above in Message object
set CREATE as eventType in Message object
}
else if("UPDATE".equals(eventType)
{
build DomainObjectA;
build DomainObjectB;
set above objects in Payload object
set Payload from above in Message object
set UPDATE as eventType in Message object
}
Currently, the producer need to know which domain objects to build in Payload depending on type of event, so this is not TypeSafe (as producer might try to set unwanted domain objects).
How can I make this typesafe and have a solution as shown below:
public class Message {
private String eventType;
//want to make Payload dynamic depending on the eventType
// for CREATE eventType i want CreatePayload while for UPDATE i want UpdatePayload
private String Payload;
}
public class CreatePayload implements Serializable {
private DomainObjectA a;
private DomainObjectB b;
private DomainObjectC c;
private DomainObjectD d;
}
public class UpdatePayload implements Serializable {
private DomainObjectA a;
private DomainObjectB b;
}
Any pointers are appreciated.Thank you.
in addition to the first question with working answer
I was not able to deserialize the following
private #Serialize List<ProduktSprache> produktsprachen;
the new class looks like this stored as Blob with orginaly having two fields defined as Text:
import java.io.Serializable;
import com.googlecode.objectify.annotation.Serialize;
public class ProduktSprache implements Serializable {
private static final long serialVersionUID = 1L;
private String sprache;
private String name;
private String details;
private String detailsText; // TEXT
private String detailsTextHTML; // TEXT
It's really hard to tell what's going on here, but it sounds like you have some data that was serialized (with java serialization) into the field. To figure this out, I'd try to simplify it as much as possible.
Serialized data is written as type Blob in the entity. So the first step is to change your produktsprachen field to type Blob. Now you can load the entity and you have a byte[] of data to work with.
Next, try to decode that byte[]. Use an ObjectInputStream to read the contents and inspect what you actually have present. Whatever structure you find there is what you eventually need to use as your #Serialized field type in your Objectify entity class.
I'm developing a simple system for parcel service. I don't fully grasp on how to do this part when setting status of the parcel. When you login you can see all available orders. As a courier you can mark new orders as "accepted" or "rejected". You can mark "accepted" orders as "in transit". And "in transit" orders you can mark as "delivered" or "failed to deliver". My question is do I need to create a field "status" and every time set some kind of string, or I should do a boolean field? By the way, if I would "reject" an order. How could I 'remember' that this particular courier has already rejected that particular oder and do not show it to him? Thank you for your ideas.
Create an Enum and define a variable of that type in your Order object and your business logic should do the setting and interpreting the enum values in your application.
public enum ORDER_STATUS {
ACCEPTED,REJECTED,DELIVERED,FAILED_TO_DELIVER,REJECT;
}
public class Order {
private Long id;
private ORDER_STATUS orderStatus;
}
You can make the enum as an inner static class. Its simpler and faster because you do not have to create an object of ORDER_STATUS.
public class Order {
private final String id;
private final String name;
private final ORDER_STATUS order_status;
public static enum ORDER_STATUS {
ACCEPTED,REJECTED,DELIVERED,FAILED_TO_DELIVER,REJECT;
}
}
I am new to Java and am using Java Eclipse, so please be kind! I hope I'm going to pose this question correctly so it makes sense.
I have four domains - each domain is pulling data from four different servers, hence the need to have them separate. But now I need to create a report that links all the data from the four domains into one report. Someone suggested using hashmaps, which I haven't used before. My four domains each have two fields that can be used as a key - CostCenter and Serial. The data being pulled is from machines all over the country. I need all the data for each machine in one report.
This is all being added to an existing project that creates a webpage with tabs for the user to click on for various tables and get data specific to a location, or to create a report for each page for all machines/locations. I just need to add a new link for the user to click on that will create this spreadsheet for them.
I've already created the domains (DAO, DAOImpl, DTO, and so on) and then I was going to create the combined report in my MainService.java. Here are the domains (lists) as declared in my MainService:
public List<Volume> getVolumeReport();
public List<BadFmPumps> getBadFmPumpsReport();
public List<BadCorobPumps> getBadCorobPumpsReport();
public List<McService> getMcServiceReport();
And here is data being pulled from the databases for each of them (domains):
public class Volume {
private String costCenter;
private String DAD;
private String division;
private String model;
private String serial;
private String numDispensers;
private String colorantSys;
private String CCEGals2017;
private String BACGals2017;
private String CCEGals2018;
private String BACGals2018;
private String DNR2017;
private String DNR2018;
private String DNR2019;
public class BadFmPumps {
private String costCenter;
private String model;
private String serial;
private String badFmPumps;
private String over10;
private String under10;
public class BadCorobPumps {
private String costCenter;
private String model;
private String serial;
private String badPumpCount;
public class McService {
private String costCenter;
private String model;
private String serial;
private String crChargeTotals;
private String emgCalls;
So I need to pull this data into one report wherever CostCenter + Serial matches. How do I declare the hashmaps for each object and how do I declare the key?
EDIT ----
I think I have something close here with
public List<Volume> getVolumeReport();
Map<String, Volume> VolumeMap = new HashMap<String, Volume>();
for (Volume dispenser : VolumeList)
{
String volumeKey = new StringBuilder().append(Volume.getCostCenter()).append(Volume.getSerial()).toString();
VolumeMap.put(volumeKey, dispenser);
}
Is this correct? I am getting one syntax error - the Map declaration
Map<String, Volume> VolumeMap = new HashMap<String, Volume>();
is giving me the error
Syntax error on token ";", { expected after this token
Is there something I need to change there?
There are some unusual things in your code. My guess is that you came from C# you are not using proper naming conventions see it here: https://www.oracle.com/technetwork/java/codeconventions-135099.html
You defined your method wrong, the error is not in the Map but the method definition
public List<Volume> getVolumeReport(); <-------- this
Should be
public List<Volume> getVolumeReport() {
And then close your method at its end (using }).
And inside your FOR you trying to direct access the Volume methods when you should use the variable you created: dispenser
String volumeKey = new StringBuilder()
.append(Volume.getCostCenter())
.append(Volume.getSerial())
.toString();
Should be
String volumeKey = new StringBuilder()
.append(dispenser.getCostCenter())
.append(dispenser.getSerial())
.toString();
I have created a server-client chat room style application and I'm trying to transfer a custom object which I have created containing the message and the username of the person using client/server application. To do this I have created a separate class called message which contains two static variables, one called username and one called messageText:
public class message implements Serializable{
static String username = "";
static String messageText = "";
public message(String message, String user){
System.out.println("Setting username and messageText");
username = user;
messageText = message;
}
public message(Object recievedObject){
username = ((message) recievedObject).getUsername();
messageText = ((message) recievedObject).getMessageText();
}
}
I send an object of this class from my server to a client as follows:
message sendMessage = new message(enteredText, "SERVER");
output.writeObject(sendMessage);
It is accepted by the client as follows:
message recievedMessage = (message) input.readObject();
cw.say(recievedMessage.getMessageText(), recievedMessage.getUsername());
However both methods above getMessageText() and getUsername() will return whatever the variable was initialized with on the receiving end, and return the correct values on the sending side.
A few hours of trying to fix this problem has provided no solution, so any help that you guys can give me is very, very appreciated! Thanks.
static fields are not serialized. If you make them non-static it should work
Static variables are shared by all instances of a class. That means it doesn't make much sense to serialize them.
In a message, the fields "username" and "messageText" are likely not meant to be static, unless there is only one message in existence.