could someone help me. Here is my problem :
I try to send an object with jms (this part works) and receive it with jms.
My object is quite simple. 3 String, 3 int, and a boolean.
There are no problem of connexion or anything like this. I receive the object but it's as if I received every things one by one.
Here is my MessageListener :
MessageListener listner = new MessageListener() {
public void onMessage(Message message) {
ObectToSend yo=null;
try {
if (message instanceof ObjectMessage) {
ObjectMessage myMessage = (ObjectMessage) message;
System.err.println("test");
yo = (ObectToSend) myMessage.getObject();
System.err.println("test2");
System.err.println(yo.entite + " " + yo.error + " " + yo.idGloreg + " " + yo.indPerso + " " + yo.nom + " " + yo.prenom + " " + yo.nom);
}
} catch (JMSException e) {
System.out.println("Caught:" + e);
e.printStackTrace();
}
}
};
And that my sending part :
Serializable ObectTest = new ObectToSend("pro", "enc", 134, 10, true, "yayaya", 0);
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
ObjectMessage message = session.createObjectMessage();
message.setObject(ObectTest);
connection.start();
producer.send(message);
Finally here is what I want to send (in receiver.java and sender.java) :
public static class ObectToSend implements Serializable{
private static final long serialVersionUID = 1L;
String prenom;
String nom;
int idGloreg;
int indPerso;
boolean ok;
String entite;
int error;
ObectToSend(String prenomP, String nomP, int idGloregP, int indPersoP, boolean okP, String entiteP, int errorP){
prenom = prenomP ;
nom= nomP;
idGloreg = idGloregP;
indPerso = indPersoP;
ok = okP;
entite= entiteP;
error = errorP;
}
}
My console :
test
test
test
test
test
test
test
If someone could tell me what's the problem that would be great. I don't get it. My textmessage with topic/queue/sync/async are working so nicely. It comes to object and....
It seems the problem is here :
yo = (ObectToSend) myMessage.getObject();
but.....
For future users of JMS I will answer my own question.
It was really hard to find any information as it's not explained in JMS documentation.
I found a lot of people asking how to do it but never had any answer. That's because it's not releated to JMS but to Java.
So here it goes :
If you want to use a same classe (object) like
ObectToSend yo = (ObectToSend) myMessage.getObject();
My first object (yo) is from the class ObectToSend.java in the package com.test.jms and my second object (myMessage.getObject() ) is from the package com.test.jms2. So I have an exception like "notfoundclass". And cannot cast objects.
The class ObectToSend.java should be in both projects. But you can't just copy paste with the same name (what I stupidly did).
You need to create a jar of the class used in both projects/packages and add it to both projects.
That way you use EXACTLY the same class and not a copy of it. And your 2 objects are exactly the same.
You could also use a map message. Indeed, you only have strings, ints, and booleans. You actually don't need an object. MapMessage is here for you and is much much simpler.
Here is an exemple :
// create mapMessage
message = session.createMapMessage();
// Here insert variables in properties of the message
// This can be filtred with selector
message.setStringProperty("entity", entity);
message.setStringProperty("messageFrom", messageFrom);
// Here insert variables in body of the message
//This CAN'T be filtred (what you want I think)
message.setString("title", title);
message.setString("description", description);
// Get map message
MapMessage mapMessage = (MapMessage) message;
// Here get variables of your message
String title = mapMessage.getString("title");
String description = mapMessage.getString("description");
See how simple it is ?
Related
I am setting up the Backend code of a chat messaging system for an app my group is creating using WebSockets. My goal is for our app to be able to send and receive messages in a public group chat, and also specifically Direct Message (DM) specific people with an # symbol, in front of the recipient's username.
I managed to get the public group messaging component working perfectly fine. However, I am running into an issue with the DM functionality. Let's say for example a person named "test" wrote a message to someone named "teacher" and here was what they typed: "#teacher testMessage". Ideally, I would want the program to send the message "testMessage" to "teacher" only. However, every time I would test the program using Postman, I would end up with the following error:
java.lang.NullPointerException: Cannot invoke "javax.websocket.Session.getBasicRemote()" because the return value of "java.util.Map.get(Object)" is null
From my understanding, the error means that the variable type the method is supposed to receive (in this case a String), is not what it is actually getting.
Here is the code below:
private static Map < Session, String > sessionUsernameMap = new Hashtable<>();
private static Map < String, Session > usernameSessionMap = new Hashtable<>();
#OnMessage
public void onMessage(Session session, String message) throws IOException { //The message is the the entire thing that the person types (ex: #teacher testMessage)
logger.info("Message Received: " + message); //String message = #teacher testMessage
String username = sessionUsernameMap.get(session); //String username = "test" This is the username of the person who wrote the message
if (message.startsWith("#")) {
String destUsername = message.split(" ")[0].substring(1); //destUsername = "teacher"
String realMessage = message.substring(message.lastIndexOf(" ") + 1); //realMessage = "testMessage"
sendMessageToParticularUser(destUsername, "[DM] " + username + ": " + realMessage); //puts in "teacher" for destUsername and "testMessage" for realMessage
sendMessageToParticularUser(username, "[DM] " + username + ": " + message);
}
else {
broadcast(username + ": " + message);
}
msgRepo.save(new Message(username, message));
}
private void sendMessageToParticularUser(String username, String message) {
System.out.println(message);
try {
usernameSessionMap.get(username).getBasicRemote().sendText(message); //PROBLEM RIGHT HERE WITH .get(username)
} catch (IOException e) {
logger.info("Exception: " + e.getMessage().toString());
e.printStackTrace();
}
}
I have been working on this issue for a few hours now with no luck. I would very much appreciate any help or input on this. Thank you.
This is how my code is looking :
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
String channelId = event.getChannelJoined().getId();
if (channelId.equals("904375329764814870")) {
Member member = event.getMember();
String memberName = event.getMember().getEffectiveName();
Category category = event.getGuild().getCategoryById("904364634507706468");
event.getGuild().createVoiceChannel("Coaching " + memberName).setParent(category).complete();
List<VoiceChannel> channelList = event.getGuild().getVoiceChannelsByName("Coaching " + memberName, true);
VoiceChannel channel = channelList.get(0);
event.getGuild().moveVoiceMember(member, channel).queue();
while (true){
event.getGuild().getTextChannelById("904364671467929610").sendMessage(
"Number of people currently on the channel : " + channel.getMembers().size()
).queue();
wait(3000);
}
My goal is to get the number of people currently inside the channel.
I thought the getMembers() method was the good method, but it's not working
I use the infinite while to see if the channel.getMembers().size() changes when i join/leave the channel but it still return 0.
How can i fix this ? And get the correct amount of people inside the channel as return.
The final goal of my code is to delete the channel when he's empty of user
This can be achieved by just checking the member count in the GuildVoiceUpdateEvent:
public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) {
if (channel.getName().startsWith("Coaching ")) { // check if name matches the desired name
if (channel.getMembers().isEmpty()) { // check if channel is empty
channel.delete().queue(); // delete channel
}
}
}
I got an error in my quickfixj Application. First, I got an error like this:
Out of order repeating group members
After that, I added this text into my initiator.config:
ValidateUserDefinedFields=N
ValidateIncomingMessage=N
But now I got another error in my application:
quickfix.FieldNotFound: Field was not found in message, field=55
at quickfix.FieldMap.getField(FieldMap.java:223)
at quickfix.FieldMap.getString(FieldMap.java:237)
at com.dxtr.fastmatch.marketdatarequestapps.TestMarketdataRequest.fromApp(TestMarketdataRequest.java:38)
at quickfix.Session.fromCallback(Session.java:1847)
at quickfix.Session.verify(Session.java:1791)
at quickfix.Session.verify(Session.java:1862)
at quickfix.Session.next(Session.java:1047)
at quickfix.Session.next(Session.java:1204)
at quickfix.mina.SingleThreadedEventHandlingStrategy$SessionMessageEvent.processMessage(SingleThreadedEventHandlingStrategy.java:163)
at quickfix.mina.SingleThreadedEventHandlingStrategy.block(SingleThreadedEventHandlingStrategy.java:113)
at quickfix.mina.SingleThreadedEventHandlingStrategy.lambda$blockInThread$1(SingleThreadedEventHandlingStrategy.java:145)
at quickfix.mina.SingleThreadedEventHandlingStrategy$ThreadAdapter$RunnableWrapper.run(SingleThreadedEventHandlingStrategy.java:267)
at java.lang.Thread.run(Thread.java:748)
My code for get value of symbols is :
public void fromApp(quickfix.Message message, SessionID sessionID)
throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
try {
String symbol = message.getString(Symbol.FIELD);
System.out.println(" FromApp " + message);
message.getString(TransactTime.FIELD);
// String seqNo = message.getString(MsgSeqNum.FIELD);
double bid = message.getDouble(MDEntryPx.FIELD);
double ask = message.getDouble(MDEntryPx.FIELD);
// System.out.println(seqNo + " " + message);
} catch (FieldNotFound fieldNotFound) {
fieldNotFound.printStackTrace();
}
}
I have also using this code
public void onMessage (MarketDataIncrementalRefresh message, SessionID sessionID) throws FieldNotFound{
try
{
MDReqID mdreqid = new MDReqID();
SendingTime sendingtime = new SendingTime();
NoMDEntries nomdentries = new NoMDEntries();
quickfix.fix42.MarketDataIncrementalRefresh.NoMDEntries group
= new quickfix.fix42.MarketDataIncrementalRefresh.NoMDEntries();
MDUpdateAction mdupdateaction = new MDUpdateAction();
DeleteReason deletereason = new DeleteReason();
MDEntryType mdentrytype = new MDEntryType();
MDEntryID mdentryid = new MDEntryID();
Symbol symbol = new Symbol();
MDEntryOriginator mdentryoriginator = new MDEntryOriginator();
MDEntryPx mdentrypx = new MDEntryPx();
Currency currency = new Currency();
MDEntrySize mdentrysize = new MDEntrySize();
ExpireDate expiredate = new ExpireDate();
ExpireTime expiretime = new ExpireTime();
NumberOfOrders numberoforders = new NumberOfOrders();
MDEntryPositionNo mdentrypositionno = new MDEntryPositionNo();
message.getField(nomdentries);
message.getField(sendingtime);
message.getGroup(1, group);
int list = nomdentries.getValue();
for (int i = 0; i < list; i++)
{
message.getGroup(i + 1, group);
group.get(mdupdateaction);
if (mdupdateaction.getValue() == '2')
System.out.println("Enter");
group.get(deletereason);
group.get(mdentrytype);
group.get(mdentryid);
group.get(symbol);
group.get(mdentryoriginator);
if (mdupdateaction.getValue() == '0')
group.get(mdentrypx);
group.get(currency);
if (mdupdateaction.getValue() == '0')
group.get(mdentrysize);
}
System.out.printf("Got Symbol {0} Price {1}",
symbol.getValue(), mdentrypx.getValue());
}catch (Exception ex)
{
System.out.println("error" + ex);
}
but i also get error like this
quickfix.FieldNotFound: Field was not found in message, field=55
at quickfix.FieldMap.getField(FieldMap.java:223)
at quickfix.FieldMap.getString(FieldMap.java:237)
at com.dxtr.fastmatch.marketdatarequestapps.TestMarketdataRequest.fromApp(TestMarketdataRequest.java:39)
at quickfix.Session.fromCallback(Session.java:1847)
at quickfix.Session.verify(Session.java:1791)
at quickfix.Session.verify(Session.java:1862)
at quickfix.Session.next(Session.java:1047)
at quickfix.Session.next(Session.java:1204)
at quickfix.mina.SingleThreadedEventHandlingStrategy$SessionMessageEvent.processMessage(SingleThreadedEventHandlingStrategy.java:163)
at quickfix.mina.SingleThreadedEventHandlingStrategy.block(SingleThreadedEventHandlingStrategy.java:113)
at quickfix.mina.SingleThreadedEventHandlingStrategy.lambda$blockInpacket_write_wait: Connection to 3.13.235.241 port 22: Broken pipe
and here the value i check in my message.log
8=FIX.4.2^A9=0217^A35=X^A34=7291^A49=Fastmatch1^A52=20200401-10:47:59.833^A56=MDValueTrade2UAT1^A262=VT_020^A268=02^A279=2^A55=GBP/CHF^A269=0^A278=1140851192^A270=1.19503^A271=02000000^A279=0^A55=GBP/CHF^A269=0^A278=1140851194^A270=1.19502^A271=06000000^A10=114^A
my broker have send to me the price and etc
My question is: how to fix my problem from this code ?
First, I got an error like this:
Out of order repeating group members
Your data dictionary doesn't match your counterparty's. Fix that and this will go away.
After that, I added this text into my initiator.config:
ValidateUserDefinedFields=N
ValidateIncomingMessage=N
This did not fix anything -- it HIDES your actual problem and has you looking at a new fake problem.
What you need to do:
Your configuration has this, right?
UseDataDictionary=Y
DataDictionary=path/to/FIXnn.xml
# or if FIX5:
AppDataDictionary=path/to/FIX5n.xml
TransportDataDictionary=path/to/FIXT.xml
Find your counterparty's documentation, and make sure your xml file's messages and fields match what they say they're going to send you. Make sure all repeating groups have the same fields in the same order.
Here is some documentation about how the Data Dictionary xml file is structured. It's pretty easy.
I have a question about mapping, map key and map values.
I am writing a chat program : I have a problem to add a message. I can't add a message. That puts me in a empty web page with an error(can't see the number and reason of error)
Can you tell me where is the problem ?
// add a message to a chatroom
#RequestMapping(value="/addMessageSalon/{salon}/{pseudo}/{message}", method = {RequestMethod.GET, RequestMethod.POST})
public String addMessageSalon(HttpServletRequest request, #PathVariable("salon") String chatroom, #PathVariable("pseudo") String username, #PathVariable("message") String message) {
Message mes = null;
mes.setMessage(message);
mes.setPseudo(username);
GestionMessages addition = (GestionMessages)request.getSession().getServletContext().getAttribute("gestionMessages");
Map<String, ArrayList<Message>> resultat = addition.getMessages();
Iterator<Map.Entry<String, ArrayList<Message>>> entries = resultat.entrySet().iterator();
// iteration
while(entries.hasNext()) {
Map.Entry<String, ArrayList<Message>> entry = entries.next();
if(!entries.hasNext() && !entry.getKey().contains(chatroom)) {
// if chatroom does not exist, we give an error
throw new IllegalArgumentException("Chatroom '" + chatroom + "' doesn't exist");
}
if(entry.getKey().contains(chatroom)){
ControleurPrincipal.getUsersInDataBase().add(username);
addition.getMessagesSalon(chatroom).add(mes);
break;
}
}
resultat = addition.getMessages();
return "redirect:/";
}
First of all:
Message mes = null;
mes.setMessage(message);
This will throw a NullPointerException, every time. So either that's the error you're getting, or that is not your code.
If that's your actual code, then you need to instantiate Message first, like this:
Message mes = new Message();
Instead of doing this
if(!entries.hasNext() && !entry.getKey().contains(monSalon)) {
you might want to do
if(!resultat.contains(monSalon)) {
and do it before the while.
I am working on a Spring Shell project. The tool is a command line tool to manipulate data in a database. There are commands like add user (which adds a record to a table in database). In order to execute any commands the user of the tool has to be connected to the database. I would like to be able to run this all in one line. The user of my tool should be able to write a command like the following.
--database connection string xyz --username abc --password mno add user --username bob --role AA_ADMIN --company Microsoft
Here the three parameters database connection string, username and password are required to run the add user command.
Below I have included some sample code it is from the spring shell reference docs
package commands;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;
#Component
public class UserManipulation implements CommandMarker {
private boolean simpleCommandExecuted = false;
#CliAvailabilityIndicator({"hw simple"})
public boolean isSimpleAvailable() {
//always available
return true;
}
#CliAvailabilityIndicator({"hw complex", "hw enum"})
public boolean isComplexAvailable() {
if (simpleCommandExecuted) {
return true;
} else {
return false;
}
}
#CliCommand(value = "hw simple", help = "Print a simple hello world message")
public String simple(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
#CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello", specifiedDefaultValue="At work") final String location) {
simpleCommandExecuted = true;
return "Message = [" + message + "] Location = [" + location + "]";
}
#CliCommand(value = "hw complex", help = "Print a complex hello world message")
public String hello(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final String message,
#CliOption(key = { "name1"}, mandatory = true, help = "Say hello to the first name") final String name1,
#CliOption(key = { "name2" }, mandatory = true, help = "Say hello to a second name") final String name2,
#CliOption(key = { "time" }, mandatory = false, specifiedDefaultValue="now", help = "When you are saying hello") final String time,
#CliOption(key = { "location" }, mandatory = false, help = "Where you are saying hello") final String location) {
return "Hello " + name1 + " and " + name2 + ". Your special message is " + message + ". time=[" + time + "] location=[" + location + "]";
}
#CliCommand(value = "hw enum", help = "Print a simple hello world message from an enumerated value")
public String eenum(
#CliOption(key = { "message" }, mandatory = true, help = "The hello world message") final MessageType message){
return "Hello. Your special enumerated message is " + message;
}
enum MessageType {
Type1("type1"),
Type2("type2"),
Type3("type3");
private String type;
private MessageType(String type){
this.type = type;
}
public String getType(){
return type;
}
}
}
So currently, hw simple is a command that is required to be executed before running hw complex or hw enum command. I do not want hw simple to be a command instead it the message parameter within the hw simple command should be a parameter that is required as a prerequisite to run hw complex or hw enum. So for example the command that I would like to run is.
--message hw complex --message abc --name1 def --name2 ghi --time 7:98 --location: Seattle
Does anyone know how to do this? If it is not possible to do this I would like to hear that or any alternative ideas if possible.
You have 2 options here:
either make those 3 additional parameters (database, username, password) parameters of each and every command that require them (note that in your particular example, you would need to rename one of those username parameters [the one to connect to the DB, or the one that represents the user to add] as you can't have 2 parameters with the same name obviously).
Use the #CliAvailabilityIndicator approach, similar to what is described in the example, where a first command (maybe named use or connect) first tests the connection with the 3 given parameters and stores them somewhere, so that any further "real" command (such as add user) can use those values.
Also note that you can actually use a combination of the two (i.e. use solution 2 to provide defaults, that may be overridden on a case by case basis by solution 1).
Lastly, please note that you'll never be able to have something like what you describe at the beginning of your question, as command names must be at the beginning and they can't contain -- (options do)